TIMEX
TIMEX

Reputation: 272264

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?

Upvotes: 0

Views: 106

Answers (1)

JamesO
JamesO

Reputation: 25966

Sure it can be done more efficiently but...

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

Upvotes: 1

Related Questions