简体   繁体   中英

How would I perform this query in Django?

class ExternalUser(models.Model):
    user = models.ForeignKey(User)
    external_account_id = models.CharField(max_length=200, null=True, blank=True)

class ExternalFriends(models.Model):
    external_user = models.ForeignKey(ExternalUser)
    external_account_id = models.CharField(max_length=200, null=True, blank=True)

Suppose I first get all ExternalFriends:

all_friends = ExternalFriends.objects.all()

OK, now I have a query set.

How do I SELECT * FROM ExternalUser WHERE external_account_id IN [ ...all_friends's external_account_ids... ] ?

What would be the Django equivalent?

当然可以更高效地完成,但是...

ExternalUser.objects.filter(external_account_id__in=[friend.id for friend in all_friends])

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