Hi I'm having trouble customizing authentication in Django
I am a newbie and I am designing Django authentication software My problem is that I have to check four separate levels for which there are predefined codes in the permission check. A model class named province and a model class named city and a model named Student My review model is such that I should be able to give a user the membership of the central office or the membership of a province or the membership of a city in a province. The user can only be for one of these situations
My models is like this
class Province(models.Model):
province_id = models.IntegerField(primary_key=True, serialize=True, verbose_name='ID')
province_title = models.CharField(max_length=30, verbose_name=_('Province'))
class Meta:
ordering = ['province_id']
def __str__(self):
return self.province_title
class Center(models.Model):
id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
center_title = models.CharField(max_length=150, verbose_name=_('Name of the training center'), null=True)
center_title_id = models.CharField(max_length=64, verbose_name=_('Code of the training center'), null=True)
province = models.ForeignKey(Province, on_delete=models.CASCADE, verbose_name=_('Province'))
class Meta:
verbose_name = _("center")
verbose_name_plural = _("centers")
def __str__(self):
return self.center_title`
class CulturalUser(AbstractUser):
pass
class UserPositions(models.Model):
class Position(models.TextChoices):
UNIVERSITY_STUDENT = '1', _('University student')
TRAINING_CENTER = '2', _('Training center')
PROVINCE = '3', _('Province')
CENTRAL_OFFICE = '4', _('Central office')
user = models.ForeignKey(CulturalUser, on_delete=models.CASCADE, verbose_name=_('U开发者_Python百科ser'))
position = models.CharField(max_length=2, verbose_name=_('Access level'), choices=Position.choices,
default=Position.UNIVERSITY_STUDENT)
province = models.ForeignKey(Province, on_delete=models.SET_NULL, blank=True, null=True)
center = models.ForeignKey(Center, on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return self.position
please direct me
According to the search I did, I could not find the right sources in my question mode
You can either extend or substitute an user model. Quotting the documentation:
If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you. This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises:
A full example
Django's user models counts with permissions and authorization and groups out of the box. So, instead of choices fields you would have four groups, for which you would handle access with permissions.
- University student
- Training center
- Province
- Central office
Here is one version of your model:
from django.utils.translation import gettext_lazy as _
from django.db import models
from django.contrib.auth.models import AbstractUser
class Province(models.Model):
name = models.CharField(
max_length=30,
verbose_name=_('Province')
)
class Meta:
ordering = ['id']
def __str__(self):
return self.title
class Center(models.Model):
name = models.CharField(
max_length=150,
verbose_name=_('Name of the training center'),
null=True,
)
code = models.CharField(
max_length=64,
verbose_name=_('Code of the training center'),
null=True,
)
province = models.ForeignKey(
Province,
on_delete=models.CASCADE,
verbose_name=_('Province')
)
class Meta:
verbose_name = _("center")
verbose_name_plural = _("centers")
def __str__(self):
return self.name
class User(AbstractUser):
province = models.ForeignKey(
Province,
on_delete=models.SET_NULL,
blank=True, null=True,
)
center = models.ForeignKey(
Center,
on_delete=models.SET_NULL,
blank=True, null=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
Note that I used AbstractUser, like you did, but you can do an even deeper customization with AbstractBaseUser, as shown in the example I linked above.
精彩评论