Thursday, October 18, 2012

ordering by generic foreign key

Hi, I'm not sure to understand what I'm doing here :) I have a model of a photo with name, description, image field... and a generic foreign key called 'likes' to store the photo in users' favorites:
likes = generic.GenericRelation(Like)

and in the Like model I have:
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()

Now when I want to display a user's favorites I can do:
q = Q(likes__user=user)
favorites = Photo.objects.filter(q)

it works ok but the result is ordered by Photo pk so if a user likes a photo that has been uploaded a long time ago it appears very far away in the favorites list. I would like to order that list by the pk of the Like object but I have no idea how to do that.
I don't know if I made myself clear. Any help is welcome.
Cheers,
Bastian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/FNUvZ6NEOrwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate