简体   繁体   English

如何在Django中基于Datefield过滤查询集?

[英]how to filter queryset based on Datefield in django?

I need to filter queryset based on today/tommorows date and the field available is a date field not datetime field 我需要根据今天/明天的日期来过滤查询集,并且可用字段是日期字段而不是日期时间字段

I am using custom manager for providing filtered result 我正在使用自定义管理器来提供过滤结果

class CallbackReqManager(models.Manager):
def get_queryset(self):
    return super().get_queryset().filter()

def callbacktoday(self):
    today=date.today

    print(today)
    return self.filter(prefereddate=today)


class CallbackReq(models.Model):

    name    =   models.CharField(max_length=50)
    email   =   models.EmailField(max_length=254)
    query   =   models.CharField(max_length=150)
    preferedtime = models.TimeField(auto_now=False, auto_now_add=False)
    prefereddate = models.DateField(auto_now=False, auto_now_add=False)
    requestedon  = models.DateTimeField(auto_now_add=True)
    attended  =   models.BooleanField(default=False)  #   whether call back is met

    objects =   CallbackReqManager()

but I am getting 但我越来越

TypeError:  match = date_re.match(value)

TypeError: expected string or bytes-like object

You may want to write like this 您可能想这样写

today = date.today()

Let me know if some error still occurs. 让我知道是否仍然出现错误。

You can add to new managers to your models, one for filtring by today's date and the other is by tomorrow's data. 您可以将新的经理添加到模型中,一个可以通过今天的日期进行过滤,而另一个可以通过明天的数据进行过滤。 Here is an example 这是一个例子

from datetime import date, timedalte
from django import models

class FilterByToday(models.Manager):
def get_queryset(self):
    """Filtering by today's date"""
    return super().get_queryset().filter(
        prefereddate=date.today()
    )

class FilterByTomorrow(models.Manager):
    """
    filtering by tomorrow's date
    where: date.today() + timedelta(days=1)
    is: Today + 1 day
    """
    def get_queryset(self):
        return super().get_queryset().filter(
            prefereddate=date.today() + timedalte(days=1)
        )


class CallbackReq(models.Model):

    name    =   models.CharField(max_length=50)
    email   =   models.EmailField(max_length=254)
    query   =   models.CharField(max_length=150)
    preferedtime = models.TimeField(auto_now=False, auto_now_add=False)
    prefereddate = models.DateField(auto_now=False, auto_now_add=False)
    requestedon  = models.DateTimeField(auto_now_add=True)
    attended  =   models.BooleanField(default=False)  #   whether call back is met
    # You can keep your default objects manager
    objects =   models.Manager()
    # Today's manager
    objects_today = FilterByToday()
    # Tomorrow's manager
    objects_tomorrow = FilterByTomorrow()

So, in your next code you can use, for example: 因此,在您的下一个代码中,您可以使用例如:

# Return all the Model's records without using the custom managers
instance = CallbackReq.objects.all()
# Return all Model's records by filtering them by today's date
instance = CallbackReq.objects_today.all()
# Return all Model's records by filtering them by tomorrow's date
instance = CallbackReq.objects_tomorrow.all()

For more informations visit the official documentation 有关更多信息,请访问官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM