我有以下型号:
class UserProfile(models.Model):
user = models.OneToOneField(User)
class Property(models.Model):
user = models.ForeignKey(User)
我想创建一个 TabularInline
在其 Django 管理页面上显示连接到特定 UserProfile
的每个属性。这里的问题当然是那个Property没有直接指向UserProfile
的ForeignKey
,所以我不能简单地写
class PropertyTabularInline(admin.TabularInline):
model = Property
class UserProfileAdmin(admin.ModelAdmin):
inlines = (PropertyTabularInline,)
我怎样才能轻松地做我想做的事?
最佳答案
您可以覆盖用户管理页面以显示 Profile
和 Property
模型。
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from myapp.models import *
class ProfileInline(admin.TabularInline):
model = Profile
class PropertyInline(admin.TabularInline):
model = Property
class UserAdmin(UserAdmin):
inlines = (ProfileInline, PropertyInline,)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
您还可以从显示中删除任何不需要/未使用的用户属性(例如组或权限)
更多:https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#extending-the-existing-user-model
这里: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example
关于python - Django admin 中的间接内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32561595/