[Fixed]-Nested serializer does not serialize the second serializer

1👍

If you look at the documentation of Reverse Relations:

Note that reverse relationships are not automatically included by the ModelSerializer and HyperlinkedModelSerializer classes. To include a reverse relationship, you must explicitly add it to the fields list.

You’ll normally want to ensure that you’ve set an appropriate related_name argument on the relationship, that you can use as the field name.

If you have not set a related name for the reverse relationship, you’ll need to use the automatically generated related name in the fields argument.

Considering the second point, you will need to add the related_name in the budget field of the Payment model:

budget = models.ForeignKey(Budget, null=True, blank=True, related_name='payments')
👤AKS

0👍

I missed related_name.

class Payment(AbstractModelController):

    class PaymentState(DjangoChoices):
        Paid = ChoiceItem("P")
        Unpaid = ChoiceItem("U")

    budget = models.ForeignKey(Budget, related_name='payments', null=True, blank=True)
    description = models.CharField(max_length=255)
    ratio = models.SmallIntegerField(validators=[validate_boundary], verbose_name="Ratio(%)")
    state = models.CharField(max_length=1, choices=PaymentState.choices, validators=[PaymentState.validator])
👤joe

Leave a comment