简体   繁体   中英

Complex query in Django ORM

I have a model Follow :

class Follow(BaseModel):

    follower = django.db.models.ForeignKey(
        'User',
        related_name='_follows_we_are_follower'
    )
    followee = django.db.models.ForeignKey(
        'User',
        related_name='_follows_we_are_followee'
    )
    datetime_canceled = django.db.models.DateTimeField(null=True, blank=True)

When I'm handling a user, how do I get all the users he's following? I assume I want something like "Give me all the users for which exists a Follow object with a datetime_canceled of None , and the follower is that user, and then for all those follows get me the users that are in the followee field." How do I write this in Django?

I'm quite away from django now So, syntax would be wrong. But I guess you want something like this:

User.objects.filter(Follow_related_name__in = ['_follows_we_are_follower', ,'_follows_we_are_followee'], datetime_canceled__is_null =True)
my_user._follows_we_are_follower.filter(datetime_canceled=None)

编辑

User.objects.filter(_follows_we_are_followee__follower=my_user, _follows_we_are_followee__datetime_canceled=None)

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