I want to get all prodcut table values with join product_ratings table. I did somthing like this but this code give me AttributeError
. So I did product_ratings = ProductRatingSerializer(many=True)
in product serializer and used this value in the field, but it’s not working:
The full error message:
Got AttributeError when attempting to get a value for field `product_ratings` on serializer `ProductSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `Product` instance. Original exception text was: 'Product' object has no attribute 'product_ratings'.
view :
class StoreApiView(mixins.CreateModelMixin, generics.ListAPIView): lookup_field = 'pk' serializer_class = ProductSerializer def get_queryset(self): qs = Product.objects.all() query = self.request.GET.get('q') if query is not None: qs = qs.filter( Q(title__icontains=query) | Q(description__icontains=query) ).distinct() return qs
its serializer classes:
class ProductRatingSerializer(ModelSerializer): class Meta: model = Product_ratings fields = [ 'p_id', 'commenter', 'comment', 'rating', 'created_date', ] read_only_fields = ['p_id'] class ProductSerializer(ModelSerializer): product_ratings = ProductRatingSerializer(many=True) author = serializers.SerializerMethodField() def get_author(self, obj): return obj.author.first_name class Meta: model = Product fields = [ 'product_id', 'author', 'category', 'title', 'description', 'filepath', 'price', 'created_date', 'updated_date', 'product_ratings', ] read_only_fields = ['product_id', 'created_date', 'updated_date', 'author']
related model class :
class Product(models.Model): product_id = models.AutoField(primary_key=True) author = models.ForeignKey(User, on_delete=models.CASCADE, db_index=True) category = models.ForeignKey(Category, on_delete=models.CASCADE, to_field='cat_id') title = models.CharField(max_length=120) description = models.TextField(null=True, blank=True) price = models.CharField(max_length=50, null=True, blank=True) filepath = models.CharField(max_length=100, null=True, blank=True) created_date = models.DateTimeField(auto_now_add=True) updated_date = models.DateTimeField(auto_now=True) class Product_ratings(models.Model): p_id = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id') commenter = models.ForeignKey(User, on_delete=models.CASCADE) comment = models.CharField(max_length=200, null=True, blank=True) rating = models.IntegerField(null=True, blank=True) created_date = models.DateTimeField(auto_now_add=True)
Advertisement
Answer
Default reverse lookup name for ForeignKey is <mode>_set
or product_ratings_set
in your case, so you need to replace product_ratings
field in ProductSerializer
with product_ratings_set
:
class ProductSerializer(ModelSerializer): product_ratings_set = ProductRatingSerializer(many=True) ... class Meta: model = Product fields = [ ... 'product_ratings_set' ]
Also you can add related_name='product_ratings'
attribute to model’s ForeignKey to change reverse lookup name, in this case you don’t need too change serializer:
class Product_ratings(models.Model): p_id = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id', related_name='product_ratings')