11

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?

5 Answers 5

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.

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.

2

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

0

You can check:

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

2
  • 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, 2011 at 9: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. Jan 20, 2011 at 9:32
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.