Reputation: 211
This problem seems to be very simple, yet i can't find solution. I needed to extend my User model (add phone number) in django, and i picked the way of creating another model called UserInfo that is related 1to1 with User model. It works fine, the only problem is that I can't get the UserInfo fields (the phone number) to show up on User page in admin panel. What i tried:
from app.models import UserInfo
from django.contrib import admin
from django.contrib.auth.models import User
class UserInfoInline(admin.TabularInline):
model = UserInfo
class UserAdmin(admin.ModelAdmin):
inlines = [UserInfoInline,]
admin.site.register(UserInfo)
EDIT: The current situation is:
try:
admin.site.unregister(User)
except admin.sites.NotRegistered:
pass
try:
admin.site.register(User, UserAdmin)
except:
pass
yet i still get the errors, this time from the sole django module:
python3.6/site-packages/django/contrib/admin/sites.py", line 109, in register
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
django.contrib.admin.sites.AlreadyRegistered: The model User is already registered
Upvotes: 3
Views: 2927
Reputation: 4664
What you should do is unregister default User and register the UserAdmin
:
from app.models import UserInfo
from django.contrib import admin
from django.contrib.auth.models import User
class UserInfoInline(admin.TabularInline):
model = UserInfo
class UserAdmin(admin.ModelAdmin):
inlines = [UserInfoInline,]
# below lines should be added
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Try deleting registering part of UserInfo
EDIT: Try following this example which is a working one:
models.py
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
STUDENT = 1
TEACHER = 2
SUPERVISOR = 3
ROLE_CHOICES = (
(STUDENT, 'Student'),
(TEACHER, 'Teacher'),
(SUPERVISOR, 'Supervisor'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True)
birthdate = models.DateField(null=True, blank=True)
role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
def __str__(self): # __unicode__ for Python 2
return self.user.username
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
instance.profile.save()
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import Profile
class ProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'Profile'
fk_name = 'user'
class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )
def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
You can read more about it here.
Upvotes: 3
Reputation: 599610
You need to re-register the User admin.
try:
admin.site.unregister(User)
except admin.sites.NotRegistered:
pass
admin.site.register(User, UserAdmin)
Upvotes: 0