0

In Django I have a 1 input form that is created like this:

class ListTripsForm(forms.Form):
    date_to_edit = forms.DateField(input_formats=['%m/%d/%Y'],
                                   widget=forms.TextInput(attrs={
                                        'class': 'form-control',
                                        'id': 'trips_month'}),
                                   initial=date.strftime(date.today(),
                                                         '%m/%d/%Y'))

The user selects a date (eg. 5/26/2017).

In the DB I have a datetime column.

Trying to get all the 'trips' from that day.

I've tried a few different ways and none have lead to success.

I am trying to add a filter to select just results from that day:

date = request.POST.get('date_to_edit', '')  # users submitted date mm/dd/yyyy
user_trips_for_date = Trip.objects.filter(user_id=user.id, trip_date=datetime.date(date))

What's the best way to accomplish this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Hanny
  • 2,078
  • 6
  • 24
  • 52
  • https://stackoverflow.com/questions/4668619/django-database-query-how-to-filter-objects-by-date-range could be of help – tushortz May 26 '17 at 18:24

1 Answers1

1

First off, you'll have an easier time of things if you instantiate your form as normal and access the field rather than extracting it directly from request.POST. Part of the point of the form classes is to handle type conversions.

Once you have that, if trip_date is a DateTimeField:

form = ListTripsForm(request.POST)
user_trips_for_date = Trip.objects.filter(user_id=user.id, trip_date__date=form.cleaned_data['date_to_edit'])

Plus the usual validation code of course, the point is to a) use the form to get a datetime.date instance and then b) use a __date filter clause.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • 1
    I think you mean `form.cleaned_data['date_to_edit']`. – Daniel Roseman May 26 '17 at 18:27
  • Hey, that worked great! it doesn't pull up the appropriate responses from the DB however. Would that be because it doesn't include the time portion? For example: this input is a DateField, so I think by default it's assigned 00:00:00:0000 for the time. The items recorded in the DB actually have hours/minutes assigned to them - so they don't match up when filtered. Does that seem accurate? – Hanny May 26 '17 at 18:41
  • The `__date` part is supposed to tell the database that the query should match on year/month/date only and not worry about the time: https://docs.djangoproject.com/en/1.11/ref/models/querysets/#date What you're describing is what I'd expect to see if your filter clause were `trip_date=form.cleaned_data...` – Peter DeGlopper May 26 '17 at 18:45
  • Alright. I'll keep playing around - this was ultimately very helpful and the right answer. I think it's just something else going on that I haven't identified yet (which will probably have me saying, "duh"). Thanks again! – Hanny May 26 '17 at 18:55
  • Odd. If I make it `trip_date=form.cleaned_data['date_to_edit']` it will find the one I entered with 00:00:00000 timestamp. If I set it to `trip_date__gt=form.cleaned_data['date_to_edit']` then it finds one of the other trips I put in that wasn't set to 00:00:0000. But if I set it to `trip_date__date=form.cleaned_data['date_to_edit']` it doesn't find anything. Strange. – Hanny May 26 '17 at 20:19
  • Yeah, sounds odd. https://stackoverflow.com/questions/1317714/how-can-i-filter-a-date-of-a-datetimefield-in-django has some answers for older versions of Django that you might be able to adapt, but personally I'd probably set up a debugger and look at exactly what the objects are - confirm that `cleaned_data['date_to_edit']` is a `datetime.date` instance, then check the SQL as shown here: https://stackoverflow.com/questions/3748295/getting-the-sql-from-a-django-queryset – Peter DeGlopper May 26 '17 at 22:17