views:

961

answers:

1
+1  Q: 

inlines -django

I am having 2 models.i want to have model2 inline with model1 .On the admin page I want to show some fields of model 2 as a inlines and all of them as readonly.ALso when i click on the value of the inline i should link me to the model2 with that value

A inline that show fields readonly .I want to show inline model fields as readonly

+2  A: 

This is (mostly) easy to do, thanks to newforms admin. Basically, you'll need to create a custom inline subclass and override the template used to render it in the admin. Assuming you have an app called app and models Model1 and Model2, you'd do the following:

First, create your admin.py file:

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 the inline.html template at 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>

Next, add your app to INSTALLED_APPS in settings.py -- don't forget to add django.contrib.admin too :).

Finally, edit your root urls.py to 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. Note that admin.site.urls, which makes the url reversal possible, will only work post django 1.1.

elo80ka
Not what I was looking for, but very cool. I'll have to add that to my book of tricks.
T. Stone