0

In my Django application, the user provides a unique product name that matches a field of another object and then displays the second object on the list (that the user sees). So the object's display condition looks something like this

obj1.name == obj2.name

It is extremely simple for one object but how to use it to get queryset (where the field of ​​objects 1 is equal to the field of object 2)?

An example of the effect I want to get:

Obj1.objects.all() = ('AS2', 'AS9', 'AD5', 'AG1')
Obj2.objects.all() = ('DD1', 'AS2', 'AS9', 'AP33', 'AD5', 'AG1', 'KQ1', 'LG4')

query1 = Obj1.objects.all()
query2 = obj2.objects.filter(name=???) 

#and query2 return all objects product which name=name well ('AS2', 'AS9', 'AD5', 'AG1') but from Obj2

1 Answer 1

2

You can try something like that:

obj_1_names = Obj1.objects.values_list('name', flat=True)
query = Obj2.objects.filter(name__in=obj_1_names)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.