How can I filter a date of a DateTimeField in Django?

I am trying to filter a DateTimeField comparing with a date. I mean: MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22)) I get an empty queryset list as an answer because (I think) I am not considering time, but I want “any time”. Is there an easy way in Django for doing this? I have the time in the datetime setted, it … Read more

Django values_list vs values

In Django, what’s the difference between the following two: Article.objects.values_list(‘comment_id’, flat=True).distinct() vs Article.objects.values(‘comment_id’).distinct() My goal is to get a list of unique comment ids under each Article. I’ve read the documentation (and in fact have used both approaches). The results overtly seem similar. 4 Answers 4

Checking for empty queryset in Django

What is the recommended idiom for checking whether a query returned any results? Example: orgs = Organisation.objects.filter(name__iexact=”Fjuk inc”) # If any results # Do this with the results without querying again. # Else, do something else… I suppose there are several different ways of checking this, but I’d like to know how an experienced Django … Read more

How to perform OR condition in django queryset?

I want to write a Django query equivalent to this SQL query: SELECT * from user where income >= 5000 or income is NULL. How to construct the Django queryset filter? User.objects.filter(income__gte=5000, income=0) This doesn’t work, because it ANDs the filters. I want to OR the filters to get union of individual querysets. 4 Answers … Read more