django user custom model authentication and authorization

Viewed 859

I have created a custom user model before making any migration and I wanted to move it from the app panel to the auth panel in the admin page.

To do that I created a proxy user model:

class User(AbstractUser):
    pass

class ProxyUser(User):
    pass

    class Meta:
        app_label = 'auth'
        proxy = True

and then in admin.py:

from django.contrib.auth.admin import UserAdmin
from .models import ProxyUser

admin.site.register(ProxyUser, UserAdmin)  

enter image description here

The problem is that the auth_permission table has permissions for user and proxyuser. enter image description here

enter image description here enter image description here

Can't understand why if I'm using a proxy and only one user table was created the permissions table behaves as if there were two (proxyuser and user). Am I missing something? Thanks in advance

2 Answers

Django uses the content type framework to keep track of "permissions" for various models. Proxy models get their own permissions.

This is explained in the authentication section of Django docs:

Proxy models work exactly the same way as concrete models. Permissions are created using the own content type of the proxy model. Proxy models don’t inherit the permissions of the concrete model they subclass

I feel what you're trying to achieve with the proxy model is unnecessary. I personally wouldn't worry much about 'Users' appearing under a separate section in the Django Admin. You will instead add unnecessary complexity to the code by using a proxy model (A future developer/you would wonder wether to use the custom User class or the ProxyUser class).

You may have done all migrations and no only for your apps, if you don't specify the app to migrate, Django makes all of the migrations. On the other way, maybe you can't log into the admin site if you doesn't do it.

Related