简体   繁体   English

如何在 Django 自定义视图中显示来自多个 model 的子集,UI 与其他模型相同?

[英]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,它由模型ArtProjectResearchProject继承,基础 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.

相关问题 我如何在 django 中显示来自标签 model 的多个标签 - How can i show multiple tag from tag model in django Django model 存储多个其他模型 存储多个其他模型 - Django model that stores multiple other models that store multiple other models 如何在同一个模型的同一个 Django 视图中保存多个表单? 当前最后一个表单在保存时覆盖 - How do I save multiple forms in the same Django View from the same Model? Currently the last form overwrites in save 如何在 Django 中连接多个模型 - How can I connect multiple models in Django 我怎样才能从其他 model 获得一个值,基于来自同一其他 model 的其他值? - How can I get a value from other model, based in other value from that same other model? Model 链接到另外两个模型。 如何访问 django 中其他两个模型的属性 - A Model is linked to two other models. How do I access the properties of that two other models in django 同一视图中的多个模型验证-Django 1.11 - Multiple models validation in the same View - Django 1.11 Django-对同一视图使用多种模型和形式 - Django - Use multiple models and forms for the same view Django - 同一表格上的多个自定义模型 - Django - Multiple custom models on the same form 一个模型如何具有多个具有与值相同类型的模型的键? - How can a model have multiple keys with the same type of models as values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM