简体   繁体   中英

Django Filter by Foreign Key

class UserTomonotomo(models.Model):

    userid= models.BigIntegerField(null=False, unique=True, db_index=True)
    email= models.CharField(max_length=100L, null=True)
    ######################
    ########

class UserFriends(models.Model):
    userid= models.ForeignKey('UserTomonotomo', to_field='userid', null=False)
    friendid = models.BigIntegerField(null=False)

I need to search for UserFriends whose userid > 0,

 UserFriends.objects.filter( userid__userid > 0 )

does not work as posted in Django - filtering on foreign key properties !! What's the fix. I am using django version 1.5.2

Thanks

use __gt :

UserFriends.objects.filter(userid__userid__gt=0)

See QuerySet API Referenece - Field lookup for other operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM