[Answered ]-Django filter based on retrieved values

2👍

✅

You can use F expressions in filters. In your case:

UserRecord.objects.annotate(time_plus_freq=F('enter_time')+F('frequency'))\
    .filter(time_plus_freq__lt=cur_time)

This way filtering is done in the SQL and you do not need to do any on the Python side.

If Django has trouble defining the type of the annotated expression look at output_field kwarg in annotate.

0👍

If you really, really want to do it directly, there is a way but it will be done on application-level, not on database-level. You can simply make generator like this:

records = (record for record in records if test(record))

but this is really bad and you should avoid it.

Much better solution is to rewrite your test condition, so it will use django queryset filtering, like it was mentioned in Ivan’s answer.

Leave a comment