joe
joe

Reputation: 9464

Nested serializer does not serialize the second serializer

I want to use nested serializer. I have followed the doc http://www.django-rest-framework.org/api-guide/relations/.

I am using Django REST 3.5.3 and Django 1.9.12.

Here is my models and serializers

models

class Budget(AbstractModelController):
    CANCELED = -1
    CREATED = 0
    QUOTATION = 1
    INVOICED = 2
    PART_PAID = 3
    COMPLETED = 4

    STATUS_CHOICES = (
        (CANCELED, _("Canceled")),
        (CREATED, _("Created")),
        (QUOTATION, _("Quotation")),
        (INVOICED, _("Invoiced")),
        (PART_PAID, _("Part Paid")),
        (COMPLETED, _("Completed"))
    )

    project = models.ForeignKey(Project, related_name="budgets", verbose_name=_("Project"))
    status = models.IntegerField(choices=STATUS_CHOICES, default=CREATED, verbose_name=_("Status"))
    name = models.CharField(max_length=200, verbose_name=_("Name"))
    value = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_("Value"))
    start_date = models.DateTimeField(verbose_name=_("Start date"))
    end_date = models.DateTimeField(verbose_name=_("End date"))

    class Meta:
        ordering = ("-id", )

    def __str__(self):
        return "{}: {}".format(self.project.name, self.name)

    def get_absolute_url(self):
        return reverse_lazy("budget:detail", kwargs={"pk": self.id})

class Payment(AbstractModelController):

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

    budget = models.ForeignKey(Budget, 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])

serializers:

class BudgetSerializer(serializers.ModelSerializer):
    project = ProjectSerializer()
    payments = PaymentSerializer(many=True, read_only=True)

    class Meta:
        model = Budget
        exclude = EXCLUDE_MODEL_CONTROLLER_FIELDS


class PaymentSerializer(ModelControllerSerializerMixin):
    class Meta:
        model = Payment
        fields = [
            "budget",
            "description",
            "ratio",
            "state",
        ]

In the payment table. It has multiple records and refer to budget_id=1

    {
        "budget": 1,
        "description": "First round",
        "ratio": 30,
        "state": "P"
    },
    {
        "budget": 1,
        "description": "Second round",
        "ratio": 30,
        "state": "U"
    },
    {
        "budget": 1,
        "description": "Final round",
        "ratio": 40,
        "state": "U"
    },
    {
        "budget": 1,
        "description": "Final round",
        "ratio": 40,
        "state": "U"
    }

And here is the budget serializer output. It does not contain nested serialized data

{
        "id": 1,
        "project": {
            "id": 1,
            "company": {
                "id": 1,
                "name": "Sarit Enterprise",
                "phone_number": "0841021711",
                "email": "[email protected]",
                "address": "bkk@bkk",
                "tax_id": "67890",
                "organization": 1
            },
            "name": "Project"
        },
        "status": 0,
        "name": "Budget",
        "value": 0.0,
        "start_date": "2016-05-17T17:00:00Z",
        "end_date": "2016-06-29T17:00:00Z"
    }

Have I missed some point of this?

Upvotes: 0

Views: 77

Answers (2)

AKS
AKS

Reputation: 19811

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')

Upvotes: 1

joe
joe

Reputation: 9464

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])

Upvotes: 0

Related Questions