如何在foreingkey django上返回关系名称

时间:2018-07-11 15:15:33

标签: mysql django

我如何从模型关系中返回名称:

code:
Inactive = 0
Active = 1

state_choices = (
    (Inactive, 'Inactive'),
    (Active, 'Active')
)
class Tipe(models.Model):

    name = models.CharField(max_length=50)
    details = models.CharField(max_length=100)
    state = models.IntegerField(
        max_length=1,
        choices=state_choices,
        default=Active,
    )

class People(models.Model):

    name=models.CharField(max_length=100)
    phone=models.CharField(max_length=9, null=True)
    state = models.IntegerField(
        max_length=1,
        choices=state_choices,
        default=Active,
    )
    tipe = models.ForeignKey(Tipe, on_delete=models.CASCADE, null=True)

问题是当我运行此命令时,如何从模型Tipe返回tipe.name:

People.objects.all().filter(state=1)

这种情况只返回我模型关系中的foreingkey dont名称。也许有任何建议请..谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

{{1}}