sort by multiple fields using admin_order_field

444 views
Skip to first unread message

alghafli

unread,
Jul 16, 2014, 2:27:50 PM7/16/14
to django...@googlegroups.com
Hello,
I made a library application. I have books each with its own id number
(different from the autofield added by django). the book has several
foreign keys each with its own id.

I wrote a function that returns a long id number in string format
consisting of the book id number and id number of several of its foreign
keys.
say I have a topic foreign key. the function does something like this:
book id: 122
topic id: 4
the function returns: 04-0122
I show this id number in the admin page. I want to sort this item which
is not actually in the database by topic id then by book id. however,
admin_order_field only takes 1 property.
if i have for example the following:
04-0122
03-1002
04-0043
they should be like this in the admin page after sorting:
03-1002
04-0043
04-0122

is that possible?
Thank you

Jorge Andrés Vergara Ebratt

unread,
Jul 16, 2014, 10:21:46 PM7/16/14
to django...@googlegroups.com
Hey, it can be done, it's something like this:

In the Model Class where you created the function you are going to add:

function_name.admin_order_field = 'The field to order'






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53C6C3AA.1010803%40gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Jorge Andres Vergara Ebratt
#SoftwareDeveloper (Or at least trying to be)
@javebratt

alghafli

unread,
Jul 17, 2014, 1:33:56 AM7/17/14
to django...@googlegroups.com
Hello Jorge
Thank you for the reply. admin_order_field can 1 and only 1 field. However, I want to sort by multiple fields. Something like this:
function_name.admin_order_field = [ 'topic id', 'book id' ]
Now the previous line does not work but I wrote it just to show the idea. if I choose to sort by the function, the admin page should sort by topic id first then it should sort by book id.
I have seen ModelAdmin.ordering which sort of works. However, this one only specifies default ordering for a model. I want to make something like this as the function ordering.

Thank you
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53C6C3AA.1010803%40gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Jorge Andres Vergara Ebratt
#SoftwareDeveloper (Or at least trying to be)
@javebratt
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages