我的模型包含用户对象作为外键。 当我在Django admin上呈现它时,它会选择模型每一行的所有用户对象(它在下拉列表中显示它们)。
如何只选择一个用户对象,然后将其用于所有行?
查询:
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
SELECT ••• FROM "users_user" ORDER BY "users_user"."email" ASC
我的管理模式:
class RoleAdmin(admin.TabularInline):
model = Role
fields = ('user', 'role_type', 'is_active',)
extra = 0
*编辑:
我尝试添加以下代码,但没有意义:
def queryset(self, request):
return super(RoleAdmin, self).queryset(request).select_related('user')
*更新:
我尝试设置raw_ids_fields但它仍然无效,因为它不会在角色和用户之间建立内部联接,而是分别对每个用户进行查询。
答案 0 :(得分:1)
Django InlineModelAdmin
(超级TabularInline
}有一个名为raw_id_fields
的选项,它不是提取所有记录并显示下拉列表,而是显示一个包含所有ID的简单文本字段所选项目以及用于浏览和取消/选择记录的按钮。 E.g。
class BookInline(admin.TabularInline):
model = Book
raw_id_fields = ("pages",)
看起来像这样 -