Django中ManyToManyField中的'through'参数是否包含所有字段?

时间:2017-06-15 11:00:55

标签: python django django-models

Django中ManyToManyField中的'through'参数是否包含相关表中的所有字段?例如,Group是否包含所有Person和Membership fileds?还有多少级别可以“通过”关系?

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

1 个答案:

答案 0 :(得分:1)

是的,当您使用直通字段时,相关模型的字段都可以从相关表中访问。级别可以尽可能深,但只是创建单独的表格会更复杂。