Django - 从内联到列表显示
Django - From inline to list display
对于下面的代码,我想在 UserAdmin
中显示最后 10 个任务 (TaskInline
),并在这 10 个任务(内联)下面有一个按钮,它会导致 TaskAdmin
任务由用户过滤。
from django.contrib import admin
from django.contrib.auth.models import User
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()
performer = models.ForeignKey(User, on_delete=models.CASCADE)
class TaskInline(admin.TabularInline):
max_num = 10 # display last 10 tasks
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
inlines = TaskInline,
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_filter = 'performer',
我该怎么做?我应该更改 TaskInline
的模板吗?
您可以使用以下代码创建表格内联:
class TaskInline(admin.TabularInline):
model = Task
extra = 1
def get_queryset(self):
qs = Task.objects.filter(user=self.request.user)
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = ['id', 'name', ...]
inlines = (TaskInline,)
注意: 您在 Task
中缺少 user
字段。
覆盖change_form.html
模板并添加
{% block inline_field_sets %}
{{ block.super }}
<a href="{% url 'admin:YOURAPP_task_changelist' %}?user__id__exact={{ object_id }}">View all tasks</a>
{% endblock %}
如有需要,这里介绍了如何仅覆盖 User
的模板
创建 html 文件 YOURAPP/templates/admin/YOURAPP/User/change_form.html
并添加
{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}
{% block object-tools-items %}
{% block inline_field_sets %}
{{ block.super }}
<a href="{% url 'admin:YOURAPP_task_changelist' %}?user__id__exact={{ object_id }}">View all tasks</a>
{% endblock %}
那么你的UserAdmin
会是这样
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
inlines = TaskInline,
change_form_template = 'admin/YOURAPP/User/change_form.html'
对于下面的代码,我想在 UserAdmin
中显示最后 10 个任务 (TaskInline
),并在这 10 个任务(内联)下面有一个按钮,它会导致 TaskAdmin
任务由用户过滤。
from django.contrib import admin
from django.contrib.auth.models import User
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()
performer = models.ForeignKey(User, on_delete=models.CASCADE)
class TaskInline(admin.TabularInline):
max_num = 10 # display last 10 tasks
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
inlines = TaskInline,
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_filter = 'performer',
我该怎么做?我应该更改 TaskInline
的模板吗?
您可以使用以下代码创建表格内联:
class TaskInline(admin.TabularInline):
model = Task
extra = 1
def get_queryset(self):
qs = Task.objects.filter(user=self.request.user)
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = ['id', 'name', ...]
inlines = (TaskInline,)
注意: 您在 Task
中缺少 user
字段。
覆盖change_form.html
模板并添加
{% block inline_field_sets %}
{{ block.super }}
<a href="{% url 'admin:YOURAPP_task_changelist' %}?user__id__exact={{ object_id }}">View all tasks</a>
{% endblock %}
如有需要,这里介绍了如何仅覆盖 User
的模板
创建 html 文件 YOURAPP/templates/admin/YOURAPP/User/change_form.html
并添加
{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}
{% block object-tools-items %}
{% block inline_field_sets %}
{{ block.super }}
<a href="{% url 'admin:YOURAPP_task_changelist' %}?user__id__exact={{ object_id }}">View all tasks</a>
{% endblock %}
那么你的UserAdmin
会是这样
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
inlines = TaskInline,
change_form_template = 'admin/YOURAPP/User/change_form.html'