jimwan
jimwan

Reputation: 1136

How to filter the datetime field in django

I have a model named Task, and in the queryset function, I want to get all the tasks by the filter of time, I want to filter (Task's end time is before now)

Below is the code, but this can now work, no tasks show after filter.

EstEndTime = models.DateTimeField('End Time', null=True)

def queryset(self, request, queryset):
    from django.utils import timezone
    now = timezone.now()
    return queryset.filter(Owner=str(current_user_name),EstEndTime = now)

Upvotes: 1

Views: 700

Answers (1)

Wolph
Wolph

Reputation: 80011

This will only return items created exactly now (to the microsecond precise depending on your database).

You are probably looking for this:

queryset.filter(EstEndTime__lte=now)

For the record, having fields named EstEndTime is against Django (and Python for that matter) naming conventions, I would recommend using a naming pattern like: est_end_time

Upvotes: 4

Related Questions