[英]How can I show Subset from multiple model in Django Custom View with the UI same as other models?
I have created a Custom view in Admin Panel with this code in admin.py.我使用 admin.py 中的代码在管理面板中创建了一个自定义视图。 I want the sometemplate.html template to have the information in the same UI format just like when I click on another model in the admin panel, and that information needs to be a combination of rows from multiple models.
我希望 sometemplate.html 模板具有相同 UI 格式的信息,就像我在管理面板中单击另一个 model 时一样,并且该信息需要是来自多个模型的行的组合。 So, the data is displayed in this custom view will have all the entries from multiple models with valid = False
因此,此自定义视图中显示的数据将包含来自多个模型的所有条目,其中 valid = False
How can I do that in my custom view of Django admin?我如何在 Django 管理员的自定义视图中执行此操作?
class DummyModelAdmin(admin.ModelAdmin):
model = DummyModel
def my_custom_view(self,request):
# return HttpResponse('Admin Custom View')
context = dict(
)
return TemplateResponse(request, "webapp/sometemplate.html", context)
def get_urls(self):
view_name = '{}_{}_changelist'.format(
self.model._meta.app_label, self.model._meta.model_name)
return [
path('my_admin_path/', self.my_custom_view, name=view_name),
]
admin.site.register(DummyModel, DummyModelAdmin)
sometemplate.html code: sometemplate.html 代码:
{% extends "admin/base_site.html" %}
{% block content %}
{% endblock %}
You could try and use django-polymorphic where you define one common base model, in their example Project
, that is inherited by the models ArtProject
and ResearchProject
, and a queryset of the base model contains instances of the base model and all models that inherit from it.您可以尝试使用django-polymorphic ,在他们的示例
Project
中定义一个公共基础 model,它由模型ArtProject
和ResearchProject
继承,基础 model 的查询集包含基础 model 的实例以及继承自的所有模型它。 Example from the docs:文档中的示例:
>>> Project.objects.all() [ <Project: id 1, topic "Department Party">, <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">, <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. >Winter"> ]
It also provides some useful model admin classes to display parent and child models in one list view.它还提供了一些有用 的 model 管理类,以在一个列表视图中显示父模型和子模型。 A lot of the django admin functionalities are preserved.
保留了许多 django 管理功能。
If you don't need any of that, you are probably better off writing the view on your own.如果您不需要这些,您最好自己编写视图。 Django admin is useful to bring some data fast to the screen, but it can get very complicated when you try to get around the default way of doing things.
Django admin 对于将一些数据快速显示到屏幕上很有用,但是当您尝试绕过默认的处理方式时,它会变得非常复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.