我在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
和权限表
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)
现在我应该使用通过字段吗?文档使用下面的示例是否与我的实现方式不同,我不需要使用直通字段?
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)
这一直令人困惑。
答案 0 :(得分:0)
所以这是正确的实现。
public function actionView($id)
{
$InsertChannel = new Channelmembers();
if (Yii::$app->request->post('submit') == 'insertdata') {
$InsertChannel->User_id = Yii::$app->user->getId();;
$InsertChannel->Channel_id = $id;
$InsertChannel->insert();
}
return $this->render('view', [
'model' => $this->findModel($id),
]);
}