Angelina
Angelina

Reputation: 363

DateTimeField without time

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?

Upvotes: 6

Views: 10351

Answers (3)

Pioute
Pioute

Reputation: 391

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

Upvotes: 17

Pablo V.
Pablo V.

Reputation: 334

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

Upvotes: 0

ecstaticpeon
ecstaticpeon

Reputation: 578

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')

Upvotes: 1

Related Questions