Arishta
Arishta

Reputation: 174

DjangoRestFramework: AttributeError: 'str' object has no attribute '_meta'

I traced this error and found that serializer.data is causing the problem. I have used similar code in my other Django apps and they are running perfectly fine.

models.py

from django.db import models

class Category(models.Model):
    name=models.CharField(max_length=30)
    def __str__(self):
        return self.name

class Subcategory(models.Model):
    category=models.ForeignKey(Category,on_delete=models.CASCADE)
    name=models.CharField(max_length=30)
    def __str__(self):
        return self.name

class Products(models.Model):
    Subcategory=models.ForeignKey(Subcategory,on_delete=models.CASCADE)
    name=models.CharField(max_length=30)
    def __str__(self):
        return self.name    

serializers.py

from rest_framework import serializers
from .models import Category,Subcategory,Products

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model='Category'
        fields='__all__'

class SubcategorySerializer(serializers.ModelSerializer):
    class Meta:
        model='Subcategory'
        fields='__all__'

class ProductsSerializer(serializers.ModelSerializer):
    class Meta:
        model='Products'
        fields='__all__'

views.py

    def post(self,request):
        action=request.query_params['action']
        if action=='add':
            category_name=request.query_params['category_name']
            category=Category(name=category_name)
            category.save()
            serializer=CategorySerializer(Category.objects.filter(name=category),many=True)
            return JsonResponse({"category details":serializer.data})

I went through all the posts on StackOverflow based on this error but none of those could help me resolve it.

Upvotes: 2

Views: 5020

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476604

The model field should refer to the model class, not a string (literal) with the name of the class, like:

from rest_framework import serializers
from .models import Category, Subcategory, Products

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'

class SubcategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Subcategory
        fields = '__all__'

class ProductsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = '__all__'

So here model = Category, not model = 'Category'. The same for the other serializers.

The logic in your post view is not entirely correct, you probably want to filter on category_name:

    def post(self,request):
        action=request.query_params['action']
        if action=='add':
            category_name=request.query_params['category_name']
            category=Category(name=category_name)
            category.save()
            serializer=CategorySerializer(Category.objects.filter(name=category_name),many=True)
            return JsonResponse({"category details":serializer.data})

Note: models are usually given singular names, so Product instead of Products.

Upvotes: 5

Related Questions