How to extend user model in Django and enable new fields in ‘newforms-admin’

The way to specify admin interface options has changed in the recent development version of Django. An inline edited objects are now specified in a new way. Now you need to mention the link in an admin objects of both models participating in the relation.

So the question arises: how to make UserProfile to appear inline in the User editing interface in admin?

The solution is to import UserAdmin class and add an InlineAdmin objet to the inlines list. But there is a simple trick: you need to unregister and then register an AdminModel class for a User model. Here is an example.

models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    website_url = models.URLField(verify_exists=False)
    user = models.ForeignKey(User, unique=True)

admin.py

from django.contrib import admin
from someapp.models import *
from django.contrib.auth.admin import UserAdmin

# Define an inline admin descriptor for UserProfile model
class UserProfileInline(admin.TabularInline):
    model = UserProfile
    fk_name = 'user'
    max_num = 1

# Define a new UserAdmin class
class MyUserAdmin(UserAdmin):
    inlines = [UserProfileInline, ]

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

Tags: ,

9 Responses to “How to extend user model in Django and enable new fields in ‘newforms-admin’”

  1. ferran Says:

    Hi, I have problem with this snippet when I added my project I have errors raise Exception(”fk_name ‘%s’ is not a ForeignKey to %s” % (fk_name, parent_model)) in many models

  2. Sasha Philippov Says:

    ferran,

    yes, there was an error in my code. I’ve updated the code above. Please, use

    class MyUserAdmin(UserAdmin):
        inlines = [UserProfileInline, ]
    admin.site.unregister(User)
    admin.site.register(User, MyUserAdmin)
    

    instead of

    UserAdmin.inlines += [UserProfileInline, ]
    admin.site.unregister(User)
    admin.site.register(User, UserAdmin)
    
  3. youlsa Says:

    Thanks for great information. That’s just what I needed to enhance my company site.

  4. Chester Says:

    Thanks! That saved me from spending another 3 days thinking on how to connect these models in admin panel.

  5. Fabian Says:

    Hello,

    thanks for the great example. Do you happen to know how you include a field from the UserProfile in MyUserAdmin’s list_display?

    search_fields = [’userprofile__nickname’] seems to work whereas list_display = (’userprofile__nickname’) does not.

    Thanks

  6. Bo Says:

    It’s possible to do that with other models beside User model? Always raise Exception(”fk_name ‘%s’ is not a ForeignKey to %s” % (fk_name, parent_model))

  7. Luke Says:

    This is a very nice piece of code. I have ready a lot about extending the user model (and I definitely felt that inheritance was a bad path for this) and I am so glad to have found this so that I can put my user profile model on the user admin page. Thanks for sharing!

  8. WALTER Says:

    Pillspot.org. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Pillspot.org.< b > < a href=”http://pillspot.org/products/vitamins_herbal_supplements/ Vitamins@buy.online” >.< /a >…

    Categories: < b >Antiviral.Mental HealthMens Health.Weight Loss.Anxiety/Sleep Aid.Womens Health.Vitamins/Herbal Supplements.Anti-allergic/Asthma.Antibiotics.Stop SmokingSkin Care.Stomach.Pain Relief.Antidepressants.Antidiabetic.Blood Pressure/Heart.Eye…

  9. LARRY Says:

    < blockquote >< a href=”http://pillspot.org/”>Pillspot.org. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. Low price pills. Buy pills online< /a >…

    Buy:Arimidex.Nexium.Retin-A.Prevacid.Accutane.Actos.Zyban.Prednisolone.Synthroid.Zovirax.Valtrex.Petcam (Metacam) Oral Suspension.Human Growth Hormone.100% Pure Okinawan Coral Calcium.Mega Hoodia.Lumigan….

Leave a Reply