i can't find a way to do the mysql "IF SELECT" with the django orm:
I have this model:
class Friendship(models.Model): to_user = models.ForeignKey(User, related_name="friends") from_user = models.ForeignKey(User, related_name="_unused_") added = models.DateField(default=datetime.date.today) objects = FriendshipManager() class Meta: unique_together = (('to_user', 'from_user'),)
Now to get all friends of user X i use this:
q = Friendship.objects.raw('SELECT IF( from_user_id = X, `to_user_id` , `from_user_id`) AS friend,id FROM `friends_friendship` WHERE `to_user_id` = X OR `from_user_id` = X'
But the raw sqls do not return any objects, only ids. So it doesn't help me at all.
How could i use the django ORM to return a queryset of users?
Best regards,