I have implemented my own User model class as follows. Note that is it NOT customizing django's auth.User
model. I am new to this object permission knowledge and especially in this self-defined User model which is required in my project.
Could you give an example of adding per-object permission in this case?
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=40, unique=True)
#.... other fields are omitted
class Article(models.Model):
title = models.CharField('title', max_length=120)
body = models.TextField('body')
author = models.ForeignKey(CustomUser)
Now, the object permission comes into play. Each user can create/update/delete/view their own article objects, but ONLY view others' articles without permission to update/delete them.
From the Django docs, the Model level permission does not apply here. If the Article is given model level update permission, then all users can update others' Articles.
I found out the django-guardian. However, there seems to be no hope for this self-defined CustomUser model, as it relies heavily on Django's auth.User
model!
https://django-guardian.readthedocs.org/en/v1.2/userguide/custom-user-model.html
- My case is subclassing AbstractBaseUser instead of AbstractUser;
- This is not for the admin but only for my backend code logic;
- I am not using Django REST API here, but if REST API is proper, please give an example.