Skip to content

Filter objects by field in custom through model in ManyToMany relationship #350

Answered by pylakey
pylakey asked this question in Q&A
Discussion options

You must be logged in to vote

I've inspected Meta.fields and this query now works:

yesterday = str(date.today() - timedelta(days=1))  # 2021-09-16
users_count = await User.objects.filter(
    (User.cities.name == "Chicago")
    & (User.usercity.visit_date == yesterday)
).count()

But it doesn't seem obvious. Field name is 'usercity" and not "user_city" as it set in Meta.tablename. What logic of such fields naming implemented in ormar now? Just lowercasing class name?

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@collerek
Comment options

Answer selected by pylakey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants