-1

I have a custom user field and a permissions field in a django app. The documentation says the following:

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

I am also currently studying this article:

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

that we are to use the manytomany through method.

But I am confused on its proper implementation. Below I have my two models. I am asking for assistance in understanding how the through field applies in this case or at all.

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

and the permissions 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)

now I am supposed to use the through fields on this? The Example the docs uses its below does it differ in a way from my implementation where I do not need to use the through field?

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)

this has been throughly confusing.

1 Answers1

0

So here is the correct implementation.

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)