In my Django app, I have a user_profile model (below).
Each user has an is_adm boolean value:
user_profile/models.py:
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField('username', max_length=50, unique=True)
email = models.EmailField('email address', unique=True)
first_name = models.CharField(max_length=20, blank=True, null=True)
last_name = models.CharField(max_length=20, blank=True, null=True)
is_adm = models.BooleanField(default=False)
Separately I have a posts model for blog posts. There is an admin panel for this model where you can add a new Post instance. You can also update, delete or view other Post instances there.
posts/models.py:
class Posts(models.Model):
title = models.CharField(max_length=300, default='')
publish_date = models.DateField(blank=True, null=True)
author = models.CharField(max_length=300)
copy = models.TextField(blank=True, default='')
link = models.URLField(blank=True)
source = models.TextField(default='', blank=True)
published = models.BooleanField(default=False)
I want the ability for is_adm = True users to be able to:
- add a Post model instance in the admin panel
- delete any Post model instance in the admin panel
- view any Post model instance in the admin panel
- edit fields in the
Postsadmin panel
I know that Django has Permissions and Authorization: https://docs.djangoproject.com/en/2.2/topics/auth/default/#permissions-and-authorization
But how do I add these methods like: has_view_permission(), has_add_permission(), has_change_permission() and has_delete_permission()...
... for is_adm = True users so that they can add, delete, view and change Post model instances through the Post admin panel?