我在 django 应用程序中有一个自定义用户字段和一个权限字段。该文档说明了以下内容:
https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ManyToManyField
我目前也在研究这篇文章:
http://charlesleifer.com/blog/describing-relationships-djangos-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
和权限表
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)
这完全令人困惑。