Inheriting from User, UserAdmin, and the Admin site

34 views
Skip to first unread message

fgasperino

unread,
Dec 11, 2009, 6:12:04 PM12/11/09
to Django users
All,

I have the following setup:

--models.py --

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

class Business (models.Model):
...

class Group (models.Model):
business = models.ForeignKey(Business)
...

class User (DjangoUser):
group = models.ForeignKey(Group)
...

--- proxies.py --

from myapp.models import Business as BusinessModel, Group as
GroupModel, User as UserModel

class Business (BusinessModel):
class Meta:
proxy = True

class Group (GroupModel):
class Meta:
proxy = True

class User (UserModel):
class Meta:
proxy = True

-- admin.py --

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from myapp.proxies import Business, Group, User

class BusinessAdmin (admin.ModelAdmin):
...

class GroupAdmin (admin.ModelAdmin):
...

class UserAdmin (DjangoUserAdmin):
...

admin.site.register(Business, BusinessAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

As you can see, I'm trying to extend on the
django.contrib.auth.models.User. This modeling works well under the
shell, where a custom user instance would be in both the built-in User
table as well as the app-specific table.

However, the admin interface has issues. Added an instance of
myapp.models.User (via myapp.proxies.User) causes an entry in the
parent table, mapped to django.contirb.auth.models.User. It does not
create a db entry for myapp.models.User.

Any suggestions on how to solve this?

TIA,

Franco

fgasperino

unread,
Dec 12, 2009, 12:19:20 PM12/12/09
to Django users
Looking into this a little deeper, it appears that from
django.contrib.auth.admin.UserAdmin uses
django.contrib.auth.models.User in the meta class. I've attempted to
point this to my app's proxy model by inheriting here as well:

-- forms.py --

from django.contrib.auth.forms import UserCreationForm as
DjangoUserCreationForm
from myapp.proxies import User

class UserCreationForm (DjangoUserCreationForm):
class Meta:
model = User

While this still hasn't solved the problem, I'm curious if the built-
in User model is more embedded than the layers I'm altering...

Franco

fgasperino

unread,
Dec 12, 2009, 3:28:52 PM12/12/09
to Django users
Solved by the Manager adding django.contrib.auth.models.UserManager to
the inherited User model.

from django.db import models
from django.contrib.auth.models import User as DjangoUser, UserManager
as DjangoUserManager

class User (DjangoUser):
objects = DjangoUserManager()

The admin site now works as expected.
Reply all
Reply to author
Forward
0 new messages