📅  最后修改于: 2023-12-03 15:14:43.030000             🧑  作者: Mango
__in
is a commonly used lookup in Django's QuerySet API. It is used to filter results based on whether or not a particular value is contained within a list or a queryset.
The __in
lookup is used to filter a QuerySet based on whether or not a particular value is contained within a specified list or a queryset.
from myapp.models import MyModel
values = [1, 2, 3, 4]
queryset = MyModel.objects.filter(id__in=values)
This will return a QuerySet containing only instances of MyModel
whose id
attribute is in the specified list of values.
from myapp.models import MyModel
related_queryset = OtherModel.objects.filter(name='foo')
queryset = MyModel.objects.filter(other_model__in=related_query)
This will return a QuerySet containing only instances of MyModel
that have a related OtherModel
instance with a name
attribute of 'foo'
.
The __in
lookup is a powerful tool for filtering QuerySets based on the contents of either a list or another queryset. It is a useful technique to have in your toolbox when working with Django's ORM.