Django, filter by specified month and year in date range

后端 未结 3 1966
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 01:04

I have the following models

class Destination_Deal(models.Model):
    name = models.CharField(_(\"Nombre\"),max_length=200)

class Departure_Date(models.Model):         


        
3条回答
  •  余生分开走
    2021-02-05 01:09

    You can get around the "impedance mismatch" caused by the lack of precision in the DateTimeField/date object comparison -- that can occur if using range -- by using a datetime.timedelta to add a day to last date in the range. This works like:

    import datetime
    
    start = date(2012, 12, 11)
    end = date(2012, 12, 18)
    new_end = end + datetime.timedelta(days=1)
    
    ExampleModel.objects.filter(some_datetime_field__range=[start, new_end])
    

提交回复
热议问题