Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering admin.ModelAdmin objects

Let's say I have my pizza application with Topping and Pizza classes and they show in Django Admin like this:

PizzaApp - Toppings      >>>>>>>>>>      Add / Change  Pizzas        >>>>>>>>>>      Add / Change 

But I want them like this:

PizzaApp - Pizzas        >>>>>>>>>>      Add / Change  Toppings      >>>>>>>>>>      Add / Change 

How do I configure that in my admin.py?

like image 367
Rui Ferreira Avatar asked Dec 29 '08 17:12

Rui Ferreira


People also ask

How do I sort fields in Django admin?

You can order the fields as you wish using the ModelAdmin. fields option. The order of fields would depend on the order in which you declare them in your models. So alternatively you could order the fields in the way you would want them to show in the admin.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


2 Answers

A workaround that you can try is tweaking your models.py as follows:

class Topping(models.Model):     .     .     .     class Meta:         verbose_name_plural = "2. Toppings"  class Pizza(models.Model):     .     .     .     class Meta:         verbose_name_plural = "1. Pizzas" 

Not sure if it is against the django's best practices but it works (tested with django trunk).

Good luck!

PS: sorry if this answer was posted too late but it can help others in future similar situations.

like image 117
Roberto Avatar answered Oct 31 '22 02:10

Roberto


If you want to solve this in 10 seconds just use spaces in verbose_name_plural, for example:

class Topping(models.Model):     class Meta:         verbose_name_plural = "  Toppings" # 2 spaces  class Pizza(models.Model):     class Meta:         verbose_name_plural = " Pizzas" # 1 space 

Of course it isn't ellegant but may work for a while before we get a better solution.

like image 26
ptronico Avatar answered Oct 31 '22 02:10

ptronico