Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering the display items by row-id in Django-Admin

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!

like image 464
samwyse Avatar asked Dec 15 '22 14:12

samwyse


1 Answers

Put the ordering on the actual model in its Meta class

class YourModel(models.Model):
    class Meta:
        ordering = ['pk']
like image 71
Nathaniel Avatar answered Jan 12 '23 04:01

Nathaniel