views:

566

answers:

2

Hi everybody I defined several models: Journals, volumes, volume_scanInfo etc.

A journal can have more volumes and a volume can have more scanInfo.

What I want to do is:

  • in the admin page of journals I want to have the list of the volumes inline (done)
  • connect each volume of the previous list to its admin page where I can show the form to edit the volume and the list of its "scan info" inline.

so I want to have something like:

Journal #1 admin page
[name]
[publisher]
[url]
.....
list of volumes inline
    [volume 10] [..(other fields)..]   <a href="/link/to/volume/10">Full record</a>
    [volume 20] [..(other fields)..]   <a href="/link/to/volume/20">Full record</a>

Then

Volume #20 admin page
[volume number]
[..(other fields)...]
......
list of the scan info inline
    [scan info 33] [..(other fields)..]   <a href="/link/to/scaninfo/33">Full record</a>
    [scan info 44] [..(other fields)..]   <a href="/link/to/scaninfo/44">Full record</a>

What I tried to do is defining a model method that create the code and trying to use it inside the class that defines "volume inline" in the admin, but it doesn't work.

In other words

the model "Volume" has inside something like:

def selflink(self):
    return '<a href="/admin/journaldb/volume/%s/">Full record</a>' % self.vid
selflink.allow_tags = True

and

class VolumeInline(admin.TabularInline):
    fields = ['volumenumber', 'selflink']
    model = Volume
    extra = 1

But this gives the following error:

Exception Value: 'VolumeInline.fields' refers to field 'selflink' that is missing from the form.

Any idea?

Thanks, Giovanni

A: 

I may be getting fields and list_display mixed up here but have you tried defining selflink() inside admin.py? Something like this:

# *** admin.py ***
def selflink(obj):
    # ...
selflink.short_description = 'Full Record'

class VolumeInline(admin.TabularInline):
    fields = ['volumenumber', selflink]
    # ...

If all you want is a link to the change page for the volume you might want to ditch the fields and go with list_display and list_display_links:

class VolumeInline(admin.TabularInline):
    list_display = ('volumenumber', 'pub_date', 'short_description',)
    list_display_links = ('volumenumber',)
    # ...
istruble
Thanks for your help.I want to do exactly what you wrote in the first part of your post but it doesn't work: it gives the same error I wrote in my question.And I think that the second part is impossible to use, because in the options of the "InlineModelAdmin" objects, there are neither "list_display" nor "list_display_links".
Giovanni Di Milia
Bummer. I looked into this a little bit more and it seems like you will have to roll your own admin for this feature. Maybe a QuickLookTabularInline(TabularInline) that supports fields and also uses a read only widget to display each field. Please update us if you find or build anything helpful.
istruble
+1  A: 

At the end I found a simple solution.

I create a new template called linked.html that is a copy of tabular.html and I added this code to create the link.

{% if inline_admin_form.original.pk %}
          <td class="{{ field.field.name }}">
              <a href="/admin/{{ app_label }}/{{ inline_admin_formset.opts.admin_model_path }}/{{ inline_admin_form.original.pk }}/">Full record</a>
          </td>
{% endif %}

then I created a new model LinkedInline inheriting InlineModelAdmin

#override of the InlineModelAdmin to support the link in the tabular inline
class LinkedInline(admin.options.InlineModelAdmin):
    template = "admin/linked.html"
    admin_model_path = None

    def __init__(self, *args):
        super(LinkedInline, self).__init__(*args)
        if self.admin_model_path is None:
            self.admin_model_path = self.model.__name__.lower()

Then when I define a new inline, I have only to use my LinkedInline instead of the normal InlineModelAdmin.

I hope it can be useful for other people.

Giovanni

Giovanni Di Milia