jartymcfly
jartymcfly

Reputation: 1995

How can I filter instances of a model in django, with a queryset value of filter field?

I am creating an application in django, and I have the next question:

I want to filet objects of modelA, by the field att1, and I have a queryset of values of the field att1. I mean, my models are:

class modelA(models.Model):
    att1 = models.ForeignKey(modelB)
    ...

class modelB(models.Model):
    ...

I got a queryset ot objects of modelB, and I want to get all objects of modelA which has as value of att1, any of the values of the queryset of modelB.

How can I do it?

Thank you so much!

Upvotes: 0

Views: 3126

Answers (2)

Patrick Tutu
Patrick Tutu

Reputation: 544

say you have object B with fields att2 and att3

class modelA(models.Model):
    att1 = models.ForeignKey(modelB)


class modelB(models.Model):
   att2 = models.CharField(max_length=255)
   att3 = models.CharField(max_length=255)

then you filter by doing: results = modelA.objects.filter(att1__att2='foo')

hope this helps

Upvotes: 0

itzMEonTV
itzMEonTV

Reputation: 20359

Nothing magic

ModelA.objects.filter(att1=queryset of modelB)

Upvotes: 1

Related Questions