在中间字段具有外键 django 的情况下建立多对多关系的正确方法

proper way to set up many to many relation where intermediary field has foreign keys django

我在 Django 应用程序中有一个自定义用户字段和一个权限字段。文档说明如下:

https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ManyToManyField

我也在研究这篇文章:

http://charlesleifer.com/blog/describing-relationships-djangos-manytomany-through/

我们要使用 manytomany through 方法。

但我对其正确实施感到困惑。下面我有我的两个模型。我寻求帮助以了解直通场在这种情况下或根本不适用。

class STUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    name = models.CharField(max_length=255)
    companyname = models.CharField(max_length=200, blank=True, null=True)
    userphoto = models.CharField(max_length=200, blank=True, null=True)
    signupvaildatestring = models.CharField(max_length=200, blank=True, null=True)
    is_active = models.BooleanField(default=False)
    phone = models.CharField(max_length=10, null=True, blank=True)
    jobtitle = models.CharField(max_length=100, null=True, blank=True)
    # password field function is provided by AbstractBaseUser

和权限 table

  class Permissions(models.Model):
    venue = models.ForeignKey(Venue, on_delete=models.DO_NOTHING, blank=True, null=True)
    user = models.ForeignKey(STUser, on_delete=models.DO_NOTHING)
    isclient = models.BooleanField(default=False)
    isvenueviewer = models.BooleanField(default=False)
    isvenueeventplanner = models.BooleanField(default=False)
    isvenueadministrator = models.BooleanField(default=False)
    issuitsviewer = models.BooleanField(default=False)
    issuitsadministrator = models.BooleanField(default=False)
    issuitssuperuser = models.BooleanField(default=False)

现在我应该在上面使用直通字段吗?文档在下面使用的示例是否与我不需要使用 through 字段的实现方式有所不同?

from django.db import models

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

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

class Membership(models.Model):
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    inviter = models.ForeignKey(
        Person,
        on_delete=models.CASCADE,
        related_name="membership_invites",
    )
    invite_reason = models.CharField(max_length=64)

这让人非常困惑。

所以这是正确的实现。

class Permissions(models.Model):
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE, blank=True, null=True)
    isclient = models.BooleanField(default=False)
    isvenueviewer = models.BooleanField(default=False)
    isvenueeventplanner = models.BooleanField(default=False)
    isvenueadministrator = models.BooleanField(default=False)
    issuitsviewer = models.BooleanField(default=False)
    issuitsadministrator = models.BooleanField(default=False)
    issuitssuperuser = models.BooleanField(default=False)

class STUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    name = models.CharField(max_length=255)
    companyname = models.CharField(max_length=200, blank=True, null=True)
    userphoto = models.CharField(max_length=200, blank=True, null=True)
    signupvaildatestring = models.CharField(max_length=200, blank=True, null=True)
    is_active = models.BooleanField(default=False)
    phone = models.CharField(max_length=10, null=True, blank=True)
    jobtitle = models.CharField(max_length=100, null=True, blank=True)
    permissions = models.ManyToManyField(Permissions, through='Authorization')
    # password field function is provided by AbstractBaseUser



class Authorization(models.Model):
    user= models.ForeignKey(STUser, on_delete=models.CASCADE)
    permission = models.ForeignKey(Permissions, on_delete=models.CASCADE)