Iterating over a or_
filter from a zip
#440
-
Following the idea presented in Ormar Filtering Section, lets suppose I have two lists: author_names = ['J.R.R. Tolkien', 'Andrzej Sapkowski']
books_since = [1960, 2000] And I would like to use a for loop to retrieve only Tolkien books newer than 1960 and Sapkowski books newer than 2000, a zip like operation. The ormar command would be something like query = Book.objects.filter(
ormar.or_(
ormar.and_(author__name = author_names[0], year__gt = books_since[0]),
ormar.and_(author__name = author_names[1], year__gt = books_since[1])
)
)
result = await query.all() How can I achieve the same with a loop over Something like (pseudo python code) query = Book.objects.filter(
ormar.or_(
for name, year in zip(author_names, books_since):
ormar.and_(author__name = name, year__gt = year),
)
)
result = await query.all() |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi! I'm terribly sorry for staying silent for so long, had family emergency, plan to catch up in following days. THANK YOU for your sponsorship, it means a lot and I really appreciate it 😁 You are almost there, you can unpack a list comprehension as both query = Book.objects.filter(
ormar.or_(
*[
ormar.and_(author__name=name, year__gt=year)
for name, year in
zip(author_names, books_since)
]
)
) |
Beta Was this translation helpful? Give feedback.
Hi!
I'm terribly sorry for staying silent for so long, had family emergency, plan to catch up in following days.
THANK YOU for your sponsorship, it means a lot and I really appreciate it 😁
You are almost there, you can unpack a list comprehension as both
and_
andor_
expect positional arguments: