Inlines-django

I have 2 models. I want to have model2 inline with model1. On the admin page, I want to show some fields of model 2 as inline strings, and all of them are read-only. Also, when I click on the inline value I have to bind me to model 2 with this value

Built-in that shows read-only fields. I want to show inline model fields as readonly

+3
source share
1 answer

This is (mostly) easy to do, thanks to newforms admin. Basically, you need to create your own built-in subclass and override the template used to render it in the admin. Assuming that you have an application called appand model Model1and Model2you must do the following:

First create a file admin.py:

from django.contrib import admin
from app.models import Model1, Model2

class Model2Admin(admin.ModelAdmin):
    list_display = (...)

class Model2Inline(admin.TabularInline):
    model = Model2
    extra = 0
    template = 'admin/app/model2/inline.html'

class Model1Admin(admin.ModelAdmin):
    list_display = (...)
    inlines = (Model2Inline,)

admin.site.register(Model1, Model1Admin)
admin.site.register(Model2, Model2Admin)

Then create a template inline.htmlin admin/app/model2:

{% load i18n %}
<div class="inline-group">
  <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
    {{ inline_admin_formset.formset.management_form }}
    <fieldset class="module">
      <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst|escape }}</h2>
      {{ inline_admin_formset.formset.non_form_errors }}
      <table>
        <thead>
          <tr>
            <th colspan="2">Field1</th>
            <th>Field2</th>
            <th>Field3</th>
          </tr>
        </thead>

        {% for inline_admin_form in inline_admin_formset %}
          <tr class="{% cycle row1,row2 %}">
            <td class="original">
              <!-- Render all form fields as hidden fields: -->
              {{ inline_admin_form.pk_field.field }}
              {% spaceless %}
              {% for fieldset in inline_admin_form %}
                {% for line in fieldset %}
                  {% for field in line %}
                    {{ field.field.as_hidden }}
                  {% endfor %}
                {% endfor %}
              {% endfor %}
              {% endspaceless %}
            </td>

            <!-- then display just the values of the fields you're interested in: -->
            <td class="field1">
              <!-- Make this a link to the change detail page for this object: -->
              <a href="{% url admin:app_model2_change inline_admin_form.original.pk %}">{{ inline_admin_form.original.field1 }}</a>
            </td>
            <td class="field2">
              {{ inline_admin_form.original.field2 }}
            </td>
            <td class="field3">
              {{ inline_admin_form.original.field3 }}
            </td>
        </tr>
      {% endfor %}
      </table>
    </fieldset>
  </div>
</div>

Then add your application INSTALLED_APPSin settings.py- do not forget to add django.contrib.admintoo :).

Finally, edit root urls.pyto include the following lines:

from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
   ...
   (r'^admin/', include(admin.site.urls))
)

That should do it. Please note admin.site.urlsthat making URL reversing possible, only django 1.1 post will work.

+7
source

Source: https://habr.com/ru/post/1708796/


All Articles