How to create RBAC Rights Management Model in myblog-django
How to create a RBAC rights management model in myblog-django? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Write the following model under user/model.py
From django.dbimport modelsfrom django.contrib.auth.modelsimport AbstractUser# Create your models here.class Menu (models.Model): name= models.CharField (max_length=25,unique=True,verbose_name=' menu name') url = models.CharField (max_length=125,unique=True,null=True,blank=True,verbose_name='URL') parent = models.ForeignKey ("self", null=True,blank=True,on_delete=models.SET_NULL,verbose_name=' parent menu') code = models.CharField (max_length=50,null=True,blank=True Verbose_name=' Encoding') icon = models.CharField (max_length=50,null=True,blank=True,verbose_name=' icon') def _ _ str__ (self): return self.nameclass Meta:verbose_name=' menu 'verbose_name_plural=verbose_namedb_table='tb_menu'class Role (models.Model): "" role binding "" name= models.CharField (max_length=35,unique=True,verbose_name=' role name') permissions = models.ManyToManyField (Menu,blank=True) Verbose_name='URL Authorization') desc = models.CharField (max_length=50,blank=True,null=True,verbose_name= "description") def _ _ str__ (self): return self.nameclass Meta:verbose_name=' role 'verbose_name_plural=verbose_namedb_table='tb_role'class UserProfile (AbstractUser): name= models.CharField (max_length=20,default= ", verbose_name=" name ") birthday = models.DateField (null=True,blank=True,verbose_name=" date of birth ") gender = models.CharField (max_length=10 Choices= (("male", "male"), ("female", "female")), default= "nale", verbose_name= "gender") image = models.ImageField (upload_to= "image/avatar/%Y/%m", default= "iamge/avatar/default.jpg", max_length=100,null=True,blank=True) roles = models.ManyToManyField (Role,verbose_name= "role") Blank=True) def _ _ str__ (self): return self.nameclass Meta:verbose_name= "user Information" verbose_name_plural=verbose_namedb_table= "tb_userprofile" ordering= ['id']
Then make the migration script
Python manage.py makemigrations
Execute the migration script
Python manage.py migrate
At this time, the data table of the database is also created successfully
This is the answer to the question about how to create a RBAC rights management model in myblog-django. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.