6
class Data(models.Model):
    time = models.DateTimeField()

I have in my variable:

var = 2013-01-17 17:30

Data.objects.filter(time=var)

I need all Data from this day(2013-01-17 - without time)

How to do?

Angelina
  • 363
  • 1
  • 5
  • 9

3 Answers3

17

I had the same problem and fixed it simply by using models.DateField() instead of models.DateTimeField().

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Pioute
  • 391
  • 3
  • 10
1

There doesn't seem to be a very straightforward way of doing this. See How can I filter a date of a DateTimeField in Django? .

You can use strptime to get a datetime out of your string ( Converting string into datetime ):

from datetime import datetime

datetime.strptime(var, '%Y-%m-%d %H:%M:%S')
Community
  • 1
  • 1
ecstaticpeon
  • 568
  • 4
  • 7
0

You can use range inside the filter

Data.objects.filter(date__range=(date_min, date_max))

you can set the time between 00.00 and 23.59

Pablo V.
  • 324
  • 2
  • 16