Django管理员内联UserProfile字段

时间:2014-04-28 22:48:50

标签: django django-admin

每个用户都有(应该拥有)UserProfile对象,并且每个UserProfile都可以有针对它的位置(位置中的外键)。我想在管理站点的用户视图中显示这些位置(并允许编辑/添加/删除它们)。嵌套的内联是不可能的,所以我想在User页面添加一个LocationInline,我不确定该怎么做。

models.py

from django.db import models
from registration.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # ...

class Location(models.Model):
    owner = models.ForeignKey(UserProfile)
    # Address and stuff

admin.py

from django.contrib import admin
from django.contrib.auth.models import User
from main.models import UserProfile, Location
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    max_num = 1
    can_delete = False

class LocationInline(admin.TabularInline):
    model = Location
    extra = 1

class UserAdmin(AuthUserAdmin):
    inlines = [UserProfileInline, LocationInline]
    # Obviously doesn't work, because Location is from UserProfile, not User
    # How can I make it use user.profile instead?

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

0 个答案:

没有答案