4

I have a ModelAdmin class with an inline of type TabularInline. What I would like is for each row of the TabularInline to have a line number displayed to the left of it. This number would increment as new records are added to the inline, and would be displayed when the form is being edited.

I prefer the line number not be a part of the model for the inlined data, but rather be generated each time a new record is added to or displayed by the inline block. I don't need to keep this number in the database. It is for reference only on another field in the ModelAdmin class.

I'm new to django, and I can't seem to figure out how to make this happen.

Any suggestions would be appreciated.

Regards, Rick

rhoward99
  • 59
  • 3
  • Have you considered using some javascript to do this on the front-end? – schillingt May 01 '14 at 23:54
  • I'm ashamed to admit it, but I've been a professional programmer since the early '80s, and this is the first web app that I have ever needed to write. Django, html, python, and javscript are all recent acquisitions for me, so I'm not sure how/where to implement the JS code. – rhoward99 May 05 '14 at 21:20
  • Gotcha. Well the quickest way to handle it is to create a javascript file and then include it on the admin as defined in the docs here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions. Using jQuery will make your life much easier on the front end and it's pretty easy to pick up. Django actually includes jQuery, but it's included as ```django.jQuery```. Finally, you should check out the #django irc channel. There's fantastic help and support there for working with Django. – schillingt May 05 '14 at 23:44
  • Thanks for the direction. I'll go off and ponder now! – rhoward99 May 06 '14 at 22:54
  • Can you share you result? – Vova Oct 13 '17 at 14:44

1 Answers1

12

You can number existing inlines easily through the admin class with a class variable and a method to return the line number:

class MyInlineAdmin(admin.TabularInline):
    line_numbering = 0
    fields = ('line_number', 'other_field')
    readonly_fields = ('line_number',)

    def line_number(self, obj):
        self.line_numbering += 1
        return self.line_numbering

    line_number.short_description = '#'

This will number any inlines in the order they appear, including any extra (blank) inlines that are included. If you add a single inline via the "Add another" link, it's line number will be correct (incremented by one from the last one), however if you add more than one inline via the link, subsequent ones will still have the same line number as the last one.

Not perfect, but better than nothing.

skulegirl
  • 346
  • 3
  • 9
  • 1
    Sometimes perfect doesn't work. Simple and effective. No playing around with row_number() (that will break down with the filtering anyway). – codeblur Nov 02 '18 at 10:14
  • Great solution! I spent hours searching for a way to count the queryset and modify the inline title, but this simple alternative meets my customer's requirements. – edepe Feb 28 '23 at 00:03