Mark Shust at M.academy
Mark Shust at M.academy

Reputation: 6429

django - add field to user profile admin form

With the given screenshot, I want to add a select dropdown to the user add page from another model that already exists on the system. how do i do this?django admin

Here is my account/models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from entities.models import Company
from entities.models import Venue

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    company = models.ForeignKey(Company)
    venue = models.ForeignKey(Venue)

    def __unicode__(self):
        return 'Test'

and here is my account/admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from entities.models import Company
from entities.models import Venue

class CompanyInline(admin.StackedInline):
    model = Company

class VenueInline(admin.StackedInline):
    model = Venue

class MyUserAdmin(UserAdmin):
    inlines = [
        CompanyInline,
        VenueInline,
    ]

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

now when I add the above code and return to the user add page, I have this error:

admin user add error

The Company and Venue models are both populated with data.

Any help would be greatly appreciated.

Thanks, Mark

Upvotes: 2

Views: 2070

Answers (1)

Mark Shust at M.academy
Mark Shust at M.academy

Reputation: 6429

Arg, this happens every time I post something up. I give the code another look-through and find the problem.

Anyways, for the other unfortunates having the same problem, I just changed my account/admin.py to the following

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from account.models import UserProfile

class UserProfileInline(admin.TabularInline):
    model = UserProfile

class MyUserAdmin(UserAdmin):
    inlines = [
        UserProfileInline,
    ]

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

Upvotes: 3

Related Questions