我想使用嵌套的序列化程序。我已按照文档http://www.django-rest-framework.org/api-guide/relations/进行了操作。
我正在使用Django REST 3.5.3和Django 1.9.12。
这是我的models
和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",
]
在payment
表格中。它有多个记录并引用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"
}
这是budget serializer
输出。它不包含嵌套的序列化数据
{
"id": 1,
"project": {
"id": 1,
"company": {
"id": 1,
"name": "Sarit Enterprise",
"phone_number": "0841021711",
"email": "cs.sarit@gmail.com",
"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"
}
我是否错过了某些观点?
答案 0 :(得分:1)
如果您查看Reverse Relations
的文档:
请注意,
ModelSerializer
和HyperlinkedModelSerializer
类不会自动包含反向关系。要包含反向关系,必须将其显式添加到字段列表中。您通常希望确保在关系上设置适当的
related_name
参数,并将其用作字段名称。如果您没有为反向关系设置
related name
,则需要在fields参数中使用自动生成的相关名称。
考虑到第二点,您需要在付款模式的预算字段中添加related_name
:
budget = models.ForeignKey(Budget, null=True, blank=True, related_name='payments')
答案 1 :(得分:0)
我错过了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])