python - Django admin 中的间接内联

标签 python django django-admin

我有以下型号:

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

class Property(models.Model):
    user = models.ForeignKey(User)

我想创建一个 TabularInline 在其 Django 管理页面上显示连接到特定 UserProfile 的每个属性。这里的问题当然是那个Property没有直接指向UserProfileForeignKey,所以我不能简单地写

class PropertyTabularInline(admin.TabularInline):
    model = Property

class UserProfileAdmin(admin.ModelAdmin):
    inlines = (PropertyTabularInline,)

我怎样才能轻松地做我想做的事?

最佳答案

您可以覆盖用户管理页面以显示 ProfileProperty 模型。

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/

相关文章:

css - Django-filer 在管理站点中呈现不正确

python - 使用 Python 导出 4 字节 float

python - 使用 scrapysplash 获取响应体

java - 从 Java 或 C# 访问一个用 Python 编写的返回数组的函数

django - 在 Django 上测试应用程序时未找到固定装置

Django Inline Formset 通过 ManyToMany 编辑一个值

python - 如何防止 TfidfVectorizer 获取数字作为词汇

python - 使用 render_to_string 在 Django 中将 ajax post 渲染为 HTML

django - 为 Django 管理页面定义自定义 css 时, 'all' 是什么意思

django - 如何修复/admin/login/处的编程错误(1146, "Table 'torquedb.showroom_customuser'不存在”)