Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position inlines in Django Admin in the list_display property?

I have two tables related Quiz and Difficulty_level:

I have created inline in admin.py like this:

class DifficultyLevelInline(admin.TabularInline):
    model = DifficultyLevel

and included in QuizAdmin

To arrange the list order, I would do:

  list_display = ('name', 'description', 'publication_date', 'category', 'is_active', 'is_premium')

How can I add inlines in the list_display order. I want to display The DifficultyLevelInline before category.

like image 547
pynovice Avatar asked Sep 11 '13 07:09

pynovice


People also ask

How do I use nested admin in Django?

First we install a package using pip: pip install django-nested-admin. Now we Add the library in settings.py: INSTALLED_APPS[ ... 'nested_admin', ... ]

Where is admin site urls in Django?

If you open a Django project's urls.py file, in the urlpatterns variable you'll see the line path('admin/', admin. site. urls) . This last path definition tells Django to enable the admin site app on the /admin/ url directory (e.g. http://127.0.0.1:8000/admin/ ).


2 Answers

Unfortunately this is not possible using the default template.

If you take a look at change_form template:

https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/change_form.html

You can see that inlines are always rendered after fieldsets.

One way to get around this would be to use other template:

class MyAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'publication_date', 'category', 'is_active', 'is_premium')
    inlines = (DifficultyLevelInline,)
    change_form_template = "my_change_form.html"
like image 89
fsw Avatar answered Oct 03 '22 08:10

fsw


Grapelli supports it: https://django-grappelli.readthedocs.org/en/latest/customization.html#rearrange-inlines

Basically, it uses a placeholder via fieldsets and then moves them HTML via JavaScript: https://github.com/sehmaschine/django-grappelli/blob/master/grappelli/templates/admin/change_form.html#L90-96 (search for placeholder, if the lines do not match anymore).

The same can be done by injecting custom javascript yourself (with or without using fieldsets as placeholders).

like image 42
blueyed Avatar answered Oct 03 '22 09:10

blueyed