Home > Enterprise >  Please is there anything i'm doing wrongly, filtered base on datetime, empty QuerySet
Please is there anything i'm doing wrongly, filtered base on datetime, empty QuerySet

Time:09-13

i got an empty QuerySet<[]>, i'd like to confirm if my models filtered are working before proceeding but seems the queryset from SubscribeEmailModel filtered from topic__startdate coming out as empty query

here is my models.py 

 class Lesson(models.Model):
    name = models.CharField(max_length=234)
    startdate = models.DateField(auto_now=True)

 class SubscribeEmailModel(models.Model):
       topic = models.ForeignKey(Lesson)

please here is my views.py

  class AutoSendNotification(ListView):
    subscriber =SubscribeEmailModel.objects.filter(topic__startdate=datetime.datetime.today(), 
          sent_mail=False)
         print(subscriber)

      model = SubscribeEmailModel
      context_object_name = 'subscriber'
      template_name = 'superadmin/email/auto-send.html'

CodePudding user response:

You are trying to filter the exact datetime with the current datetime which might not match.

If you are trying to filter out based on date then set topic__startdate__date in filter

today = datetime.datetime.today().date()
subscriber = SubscribeEmailModel.objects.filter(topic__startdate__date=today, sent_mail=False)

If you are trying to filter out based on date greater than or less than then set topic__startdate__gt or topic__startdate__lt in filter

today = datetime.datetime.today()
subscriber = SubscribeEmailModel.objects.filter(topic__startdate__lt=today, sent_mail=False)

CodePudding user response:

you're fetching date including Timestamp which can cause issue.

just make sure you have data created on current date. try this

from datetime import date

today = date.today()
subscriber =SubscribeEmailModel.objects.filter(topic__startdate=today, 
          sent_mail=False)
  • Related