1

I have two models:

class Product(models.Model):
    name = models.CharField(max_length=200)
    price = models.DecimalField(default=0, decimal_places=2, max_digits=10)

    def __str__(self):
        return self.name
class Receipt(models.Model):
    purchase_date = models.DateTimeField(auto_now=True, null=False)
    shop = models.ForeignKey(Shop, on_delete=models.SET_NULL, null=True)
    products = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return super().__str__()

My receipt url looks like this: enter image description here

And i would like it to show list of all products, instead of a number of them. How to do this?

My viewsets:

class ReceiptViewSet(viewsets.ModelViewSet):
    queryset = Receipt.objects.all()
    serializer_class = ReceiptSerializer
    permission_classes = [AllowAny]

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = [AllowAny]
    enter code here

serializers:

class ProductSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Product
        fields = ['id', 'name', 'price']

class ReceiptSerializer(serializers.HyperlinkedModelSerializer):
    products = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Receipt
        fields = ['id', 'purchase_date', 'shop', 'products']
2
  • share your serializers? Commented Dec 20, 2021 at 9:25
  • Sorry for that, i updated my question and added serializers there Commented Dec 20, 2021 at 9:31

2 Answers 2

1

You can simply specify nested representations using the depth option

Give this a try

class ReceiptSerializer(serializers.HyperlinkedModelSerializer):
    ... 

    class Meta:
        model = Receipt
        fields = ['id', 'purchase_date', 'shop', 'products']
        depth = 1

Otherwise

class ReceiptSerializer(serializers.HyperlinkedModelSerializer):
    products = ProductSerializer(many=True, read_only=True)
    ... 

    class Meta:
        model = Receipt
        fields = ['id', 'purchase_date', 'shop', 'products']
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it and it shows me that: TypeError at /receipts/ 'Product' object is not iterable
@santana011 remove the many=True and try again, by the way I don't really understand why you named the related connection in plural
@santana011 how about the first method?
It works, but i cant figure out why receipt can have only one product. Whenever i try to add another product to receipt it just switches them
Foreiginkey can have only one relation, in your scenario you can either implement a reverse foreignkey of a manytomany relation.
0

I have tried Sumithran 's solution but i made a small adjustment, rather than using serializers.HyperlinkedModelSerializer i used serializers.ModelSerializer and it displayed the products as follows View

Hope it answers what you are looking for :)

from rest_framework import serializers

class ReciptSerializer(serializers.ModelSerializer):
    class Meta:
        model = Receipt
        fields = ['id', 'purchase_date', 'shop', 'products']
        depth = 1

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

1 Comment

The change should be included in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.