0

I'm very new to python and django and I'm working on a version of the locallibrary project to learn some concepts. I've created a model with an @property to show when an item is overdue:

@property
def overdue(self):
    is_overdue = False
    if (datetime.date.today() > self.due_date) and self.status != 'c':
            is_overdue = True
    return is_overdue

I would like to get a count of all items where overdue=True. In templates I can use 'overdue' as if it were a field, however, trying to use is in a view like this...

num_actions_overdue = Action.objects.filter(overdue='True').count()

...throws an error:

Cannot resolve keyword 'overdue' into field.

Is there a simple way to count all overdue records?

Thanks in advance for any help!

Jon
  • 1
  • 1
  • FYI that method is just `return (datetime.date.today() > self.due_date) and self.status != 'c'` – jonrsharpe Apr 20 '20 at 16:04
  • As the duplicate says, you can't. You have to filter on Python's level. for example, `len(list(filter(lambda action: action.is_overdue, Action.objects.all())))` – DeepSpace Apr 20 '20 at 16:11
  • Thanks for the response folks. Guess I need to learn more about the concepts before asking questions, as I didn't spot any of those as duplicating my question beforehand :/ – Jon Apr 20 '20 at 16:45

0 Answers0