4

I have a table of static data, loaded via initial_data.json. The admin interface apparently displays the data ordered by descending pk; i.e. the records with the largest pk are at the top of list. I would like to reverse this, ideally by using the pk value. Since it's static data, I could add another column for the sort order, but that would essentially duplicate the pk values, or I could, when generating my fixture file, use "pk=len(data)-i" instead of the "pk=i" that I'm currently using. But really, I'd like to just say something like this:

class DataAdmin(admin.ModelAdmin):
    ordering = ('row_id',)

I've tried using 'row_id', 'id', and 'pk', without success. Does anyone have any ideas beffore I go source-diving? Thanks!

samwyse
  • 2,760
  • 1
  • 27
  • 38
  • See this http://stackoverflow.com/questions/3625641/django-queryset-custom-ordering-by-id – Miguel-F Jan 24 '13 at 18:15
  • Hi! **it work** for me with "ordering = ('id')" in the model admin... (using django 2.4 and python 2.7). I think it should be ordering= ('pk',) and that may be a django improvement idea. – Guillaume Gendre Feb 11 '13 at 09:21

1 Answers1

13

Put the ordering on the actual model in its Meta class

class YourModel(models.Model):
    class Meta:
        ordering = ['pk']
Nabin
  • 11,216
  • 8
  • 63
  • 98
Nathaniel
  • 666
  • 4
  • 15