Llanilek
Llanilek

Reputation: 3466

Filtering queryset by next 30 days and date passed

I'm not sure if It's due to lack of sleep, but I cannot for the life of me figure out what's causing this issue.

AttributeError: 'datetime.date' object has no attribute 'utcoffset'

I'm trying to filter a queryset based on two conditions.

  1. If the date has already passed.
  2. If the date is within the next 30 days.

Model

class Member(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
    ...
    membership_expiry = models.DateField(null=True, blank=True)
    club_membership_expiry = models.DateField(null=True, blank=True)
    medical_expiry = models.DateField(null=True, blank=True)

View get_context_data override

def get_context_data(self, **kwargs):
        context = super(MembershipReport, self).get_context_data(**kwargs)
        members = Member.objects.all()
        now = date.today()
        thirty_days = now + timedelta(days=30)
        context['membership_overdue'] = members.filter(Q(membership_expiry__lte=now) | Q(membership_expiry__gte=now, membership_expiry__lte=thirty_days))
        return context

I've tried using date.today() datetime.now() and django's timezone.now() all three throw the same error.

Upvotes: 1

Views: 281

Answers (2)

Llanilek
Llanilek

Reputation: 3466

Turns out it was lack of sleep. Posting this here because of the other answers gave me some clarity and helped fix the naive problem. However the error was actually because I was calling naturaltime on a date object instead of naturalday from the humanize lib.

Upvotes: 0

JPG
JPG

Reputation: 88689

try this,

import pytz
from datetime import datetime

now = datetime.now(tz=pytz.UTC)

Upvotes: 3

Related Questions