Django gets a unique clock in a datetime field

Is there a method that I cannot find to get individual hours in a DateTimeField? I essentially want the same thing as .dates (), but for hours.

I have to clarify what I'm talking about the QuerySet method. The date () method I'm talking about is here: http://docs.djangoproject.com/en/1.1/ref/models/querysets/#dates-field-kind-order-asc

If not, is there a recommended known solution?

Thank.

Addendum for clarification: I have a model called Event with DateTimeField called start_date. We need to know what hours of a certain day have an event. Say we shorten it to a specific month:

objects = Event.objects.filter(start_date__year=2010, start_date__month=2)

Now the date functions can give me a list of all the days in which there is an event:

distinct_days = objects.dates('start_date', 'day')

What I would like is to narrow it down to a certain day, and then get a report list of hours of the day in which there is an event.

objects = Event.objects.filter(start_date__year=2010, start_date__month=2, start_date__day=3)
distinct_hours = objects.times('start_date', 'hour') # This command doesn't exist and am wondering how to do this

Thank.

+3
source share
4 answers

Unfortunately, at the moment there is no good way to do this, the best way is to use some raw SQL in combination with the extra () method, and then call distinct ().

+1
source

Do you want to get the clock from a datetime object ? Which .dates()do you mean?

hour = instance.yourDateTimeField.hour
+2
source

, .

hours = []
for h in objects.values('start_date'):
    hours.append(h['start_date'].hour)
tempdict = {}
for x in hours:
    tempdict[x] = x
hours = tempdict.values()
hours.sort()
+1

: , unique=True.

0

Source: https://habr.com/ru/post/1732930/


All Articles