10

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, then 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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Kai
  • 2,205
  • 3
  • 32
  • 43

5 Answers5

12

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.

kirelagin
  • 13,248
  • 2
  • 42
  • 57
5

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.

alex vasi
  • 5,304
  • 28
  • 31
0

In case someone else is seeking the solution for that issue in 2017, I found the great package Django Admin Sortable

Sergei V Kim
  • 205
  • 1
  • 2
  • 7
0

You can check:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_editable

Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83
  • Sorry you might have misunderstood me. I want to change the value in the "order" field so that my view can show the items in the order I specified by doing so. With the options you outlined, I can only modify how the elements are shown in the admin view, can't I? – Kai Jan 20 '11 at 09:16
  • You're right - I've update my answer. Still, I don't think that you can easily reorder items on-the-fly, as you edit them - you still need to refresh the page. – Tomasz Zieliński Jan 20 '11 at 09:32
0

You can use django-admin-sortable2 to easily change the order of items including inline items as well.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129