Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order items with the django-admin interface

Lets say I have a django model looking like this:

class question(models.Model):
  order = models.IntegerField('Position')
  question = models.CharField(max_length= 400)
  answer = models.TextField()
  published = models.BooleanField()

  def __unicode__(self):
    return self.question

In my view I show all of the questions ordered ascending by the order field.

My question is: Is there an easy way to edit the order field in the django admin interface? Right now I have to go to edit the Question, than look up what number to put in the order field and maybe even reorder all the other items. What i really want would be some "up and down"-arrows on the admin page where all the questions are listed.

Is that possible?

like image 237
Kai Avatar asked Jan 20 '11 07:01

Kai


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.

How use Django admin panel?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


2 Answers

Check this: django-orderedmodel.

This is a really simple implementation of abstract base class for items which can be ordered with admin interface. No external dependencies and easy to use.

like image 181
kirelagin Avatar answered Nov 16 '22 12:11

kirelagin


Sure, here is an example of admin.py file with up and down links to change items order: https://github.com/alexvasi/django-simplemenu/blob/master/simplemenu/admin.py

Basically, you just need to override get_urls method to add your custom views (move_up and move_down in this example).

More famous example would be django-treemenus, but there is some extra code to support older versions of django.

like image 23
alex vasi Avatar answered Nov 16 '22 12:11

alex vasi