django-rest-framwork尝试获取字段值时出现AttributeError [英] django-rest-framwork Got AttributeError when attempting to get a value for field
问题描述
我想通过join product_ratings table获得所有prodcut表值.我做了这样的事情,但是这段代码给了我AttributeError ...我做了
i want get all prodcut table values with join product_ratings table . i did somthing like this but this code give me AttributeError ... i did
product_ratings = ProductRatingSerializer(许多=真)在产品序列化程序中,并在字段中使用了此值,但它不起作用:
product_ratings = ProductRatingSerializer(many=True) in product serializer and used this value in field but it not work:
其完整的错误消息:
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'.
视图:
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
其序列化程序类:
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']
相关模型类:
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)
推荐答案
对于您的情况,ForeignKey的默认反向查找名称为< mode> _set
或 product_ratings_set
,因此您需要将 ProductSerializer
中的 product_ratings
字段替换为 product_ratings_set
:
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'
]
还可以在模型的ForeignKey中添加 related_name ='product_ratings'
属性,以更改反向查找名称,在这种情况下,您无需更改序列化程序:
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')
这篇关于django-rest-framwork尝试获取字段值时出现AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!