Django / DRF:过滤反向外键

时间:2018-09-23 14:33:51

标签: python django django-models django-rest-framework

我希望只能过滤Django模型的关系。

这是模型的样子:

Identity(models.Model):
    username = model.CharField(max_length=512, unique=True)

PaymentSource(models.Model):
    identity = models.ForeignKey(
        'identity.identity',
        related_name='payment_sources',
        on_delete='CASCADE',
        blank=True,
    )
    deleted_at = models.DateTimeField(blank=True, null=True, default=None)

两者都有完整的序列化器代码。使用身份引用PaymentSerializer

如果PaymentSource不为None,我如何获得带有DRF的身份序列化器作为响应传递回去,而不在外键中包含任何deleted_at模型?

案件分类:

1) There should only be 1 Identity object returned

2) If there are many payment sources with deleted_at set as None, then they should be shown within the Identity.payment_sources

3) If there are none, Identity should still be returned. However an empty list should be returned from Identity.payment_sources

0 个答案:

没有答案