Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2.4K
Visualizações
django-rest-framwork got AttributeError when attempting to get a value for field

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)
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The default reverse lookup name for ForeignKey is <mode>_set or product_ratings_set in your case, so you need to replace the product_ratings field in the ProductSerializer with product_ratings_set :

 class ProductSerializer(ModelSerializer):
 product_ratings_set = ProductRatingSerializer(many=True)

 class Meta: 
 model = Product
 fields = [ ... 'product_ratings_set' ]

You can also add related_name='product_ratings' to the ForeignKey of the model to change the reverse lookup name, in this case you don't need to change the serializer too much:

 class Product_ratings(models.Model): p_id = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id', related_name='product_ratings')
over 4 years ago · Santiago Trujillo Relatório

0

in my case I had to return just single object from my views.py but I was returning queryset, so changing objects.filter to objects.get fixed the issue for me

over 4 years ago · Santiago Trujillo Relatório

0

Selected answer doesn't work for me. However following way worked:

product_ratings = ProductRatingSerializer(many=True)

Also remember to put product_ratings in related_name field like this:

 p_id = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id', related_name='product_ratings')

Here is how Meta class looks:

class Meta:
        model = Product
        fields = [
        ...
        'product_ratings'
        ]  
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda