In your Django admin, enable auth.User as a built-in

I saw a lot of questions differently that have something like the following:

class UserProfileInline(admin.StackedInline): model = UserProfile can_delete = False verbose_name_plural = 'profile' class NewUserAdmin(UserAdmin): inlines = (UserProfileInline, ) admin.site.unregister(User) admin.site.register(User, NewUserAdmin) 

What I want to do is the opposite, but I don’t see it working. Here the code that I have does not work.

 from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User from myapp.models import SpecialUserType class UserInline(admin.StackedInline): model = User can_delete = False class IncludeUserAdmin(admin.ModelAdmin): inlines = (UserInline,) admin.register(SpecialUserType, IncludeUserAdmin) 

How can I do this work so that User built into admin SpecialUserType ?

The error I am getting is:

 <class 'django.contrib.auth.models.User'> has no ForeignKey to <class 'students.models.SpecialUserType'> 

This makes sense because OneToOneField is located in the SpecialUserType model, obviously, and not in the User . But how can I make it reverse on OneToOneField ?

(I know this may seem unusual, but there is a good reason why I want to configure the administrator this way, and not vice versa).

+6
source share
1 answer

Unfortunately this is not possible. Several times I encountered a problem in front of me.

As the error message says, the embedded model must have a ForeignKey (or OneToOneField) property that points to the main model.

As already mentioned, I found the User subclass very useful for me:

 class UserProfile(User): user = OneToOneField(User, parent_link=True) # more fields, etc 
+1
source

Source: https://habr.com/ru/post/928045/


All Articles