A
A
Andrew2021-12-15 18:32:11
Django
Andrew, 2021-12-15 18:32:11

How to add Django table to change_form.html edit form?

Friends, welcome. Tell me how to add a table to the edit page in the admin panel?

There is MaterialAdmin. I rewrote the change_view() function in it. I put all the necessary data in extra_content. Now I want standard jang tables under the edit form for this data.

class MaterialAdmin(admin.ModelAdmin):
    change_form_template = 'admin/change_material_form.html'

    search_fields = ['name']

    autocomplete_fields = ("manufacturer", )
    list_display = ("_show_name_with_manufacturer", 
                "_show_material_groups_quantity",
                "_show_boxing",
                "unit",)

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related("manufacturer").prefetch_related("material_group", "material_item", "unit")
    
    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        material = Material.objects.filter(id=object_id).select_related("manufacturer").prefetch_related("material_group", "material_item", "unit").first()
        material_items = material.material_item.all()
        material_groups = material.material_group.all()
        works = MaterialGroupWork.objects.filter(material_group__in=material_groups).distinct()
        extra_context['material_items'] = material_items
        extra_context['material_groups'] = material_groups
        extra_context['works'] = works
        return super(MaterialAdmin, self).change_view(
            request, object_id, form_url, extra_context=extra_context,
        )


admin.site.register(Material, MaterialAdmin)


change_material_form.html
....
<div>
    {% for material_item in material_items %}
        {{ material_item }}
    {% endfor %}
</div>
<div>
    {% for material_group in material_groups %}
        {{ material_group }}
    {% endfor %}
</div>
<div>
    {% for work in works %}
        {{ work.work }}
    {% endfor %}
</div>


Instead of divs, you need to make standard dzhang tables. I looked at the change_list_results.html template, it seems to be logical to connect it and transfer data there, but result_headers and results come to it. How do I get this data from my queryset? Or can it be done in some other way?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question