Thank you Simon,

This func expression what was I looking for. However, some more complex 
queries with the date() function still confuse me.

For example the SQL query:

select count(distinct UniqueCallID), date(JoinTime) from Table group by 
date(JoinTime);


I tried to write it as:

calls = Call.objects.annotate(join_date=Func(F('jointime'), function='DATE'), 
count=Count('uniquecallid', distinct=True)).values('join_date', 'count')


But it seems that the group by is not working as expected and the values 
returned are not distinct. Am I missing something?


On Wednesday, 15 June 2016 04:08:48 UTC+1, Simon Charette wrote:
>
> Hi Galil,
>
> In the next version of Django (1.10) you'll be able to use the TruncDate 
> expression[1]
> for this exact purpose:
>
> MyModel.objects.annotate(join_date=TruncDate('join_time')).values_list('join_date',
>  
> flat=True)
>
> In the mean time you can simply use a Func expression[2]:
>
> MyModel.objects.annotate(join_date=Func(F('join_time'), 
> function='DATE')).values_list('join_date', flat=True)
>
> Cheers,
> Simon
>
> [1] 
> https://docs.djangoproject.com/en/1.10/ref/models/database-functions/#django.db.models.functions.datetime.TruncDate
> [2] 
> https://docs.djangoproject.com/en/1.9/ref/models/expressions/#func-expressions
>
> Le mardi 14 juin 2016 09:52:34 UTC-4, Galil a écrit :
>>
>> Hi, 
>>
>> How can I convert this SQL query into a Django query?
>>
>> SELECT DATE(JoinTime) FROM table
>>
>> Please keep in mind that JoinTime is in datetime format and I want it to 
>> be date.
>>
>> Thanks
>>
>>
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d663318b-59b6-47e5-9cbd-f08cc4db0906%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to