0
In [97]: cust = CustomerProfile.objects.get(pk=100)

In [98]: CustomerProfile.objects.filter(user__date_joined=datetime.datetime(2017, 7, 28))
Out[98]: <QuerySet []>

In [99]: CustomerProfile.objects.filter(user__date_joined=datetime.datetime(2017, 7, 28, 14, 43, 51, 925548))
Out[99]: <QuerySet [<CustomerProfile: George George's customer profile>]>
In [100]: cust
Out[100]: <CustomerProfile: George George's customer profile>
In [101]: cust.user.date_joined
Out[101]: datetime.datetime(2017, 7, 28, 14, 43, 51, 925548)

I am not sure why it is not working here just with the date 2017/07/28. Why is it empty with just the date? How could I obtain the last queryset with just the date and not all the stuff datetime.datetime(2017, 7, 28, 14, 43, 51, 925548) ?

David
  • 149
  • 1
  • 3
  • 14

2 Answers2

1

You can use __date from documentation, or look on this answer - there are a lot of good solution.

amarynets
  • 1,765
  • 10
  • 27
1

The reason you are facing this problem is because you are trying to search for a specific datetime. This means, you are looking for a combination of date and time. So, when you create the datetime.datetime object, datetime.datetime(2017, 7, 28), you are basically created a date with a time of 0 hours, 0 minutes and 0 seconds.

In order to filter just by the date only, you can use the __date attribute to compare the date only.

So, your comparison would look like:

CustomerProfile.objects.filter(user__date_joined__date=datetime.datetime(2017, 7, 28))
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199