Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex query in Django ORM

Tags:

python

orm

django

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?

like image 221
Ram Rachum Avatar asked Mar 27 '26 20:03

Ram Rachum


1 Answers

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)
like image 159
Laxmikant Ratnaparkhi Avatar answered Mar 31 '26 03:03

Laxmikant Ratnaparkhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!