How to filter model property in views?

Ok, at this point then I think we can say we’ve answered your original question.

In an expression such as SomeModel.objects.filter(some_field=some_value), some_field must be a field in the model.
Now, this means it can also be a field dynamically created within the query through an annotation.
For example, assuming the standard system User model:
User.objects.annotate(new_field=F('first_name')).filter(new_field='Ken')
is a valid query.

So, if you can convert whatever it is you’re doing to determine the value of on_sale into a Subquery that can be built into your query, then you can do this within the database.
Otherwise, that filtering will need to be done within Python.

1 Like