0

I have added a field to my User model called extra, as follows:-

class ExtendedUser(models.Model):
user = models.OneToOneField(User)
extra = models.CharField(max_length=30, null=True)

# when a user is created, make sure an ExtendedUser is created too
def create_extended_user(sender, instance, created, *args, **kwargs):
    if created:
        extended_user = ExtendedUser(user=instance)
        extended_user.save()
post_save.connect(create_extended_user, sender=User)

In my admin.py, I try to ensure that I can edit this extra field when I'm editing a user:-

class ExtendedUserInline(admin.TabularInline):
    model = ExtendedUser
    fk_name = 'user'
    max_num = 1

class ExtendedUserAdmin(UserAdmin):
    inlines = [ExtendedUserInline]

admin.site.unregister(User)
admin.site.register(User, ExtendedUserAdmin)

However, I can't see the extra field in the admin page for User.

What have I done wrong?

bodger
  • 1,112
  • 6
  • 24

2 Answers2

0

Have you restarted the development server since editing admin.py?

See also this very similar thread.

Community
  • 1
  • 1
edkay
  • 181
  • 1
  • 8
0

Your first block of code looks ok to me, although you might want to use the decorators for readability. Here's an updated example.

class ExtendedUser(models.Model):
user = models.OneToOneField(User)
extra = models.CharField(max_length=30, null=True)

# when a user is created, make sure an ExtendedUser is created too
@receiver(post_save, sender=User)    
def create_extended_user(sender, instance, created, *args, **kwargs):
        if created:
            extended_user = ExtendedUser(user=instance)
            extended_user.save()

Updated Answer:

You need to set AUTH_PROFILE_MODULE = 'app_name.ExtendedUser'

Here's a post that did pretty much the same as you. Some of the comments below are worth reading.

chirinosky
  • 4,438
  • 1
  • 28
  • 39
  • Sadly, that doesn't work. Surely the UserAdmin is the wrong class name here anyway? The one I used was ExtendedUserAdmin - as I was extending the usual UserAdmin. – bodger Feb 24 '13 at 18:12
  • You're right. Answer deleted. Going to try replicating with your code. – chirinosky Feb 24 '13 at 18:40
  • That doesn't seem to do it either. Still can't see the extra field in the admin interface. – bodger Feb 24 '13 at 20:38