Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:19:21+00:00 2026-05-29T21:19:21+00:00

Hi I have a model containing: class MyModel(models.Model): id = models.IntegerField(primary_key=True) recorded_on = models.DateField()

  • 0

Hi I have a model containing:

class MyModel(models.Model):
    id = models.IntegerField(primary_key=True)
    recorded_on = models.DateField()
    precipitation = models.FloatField(null=True, blank=True)

I have a form which looks like this:

class QueryForm(forms.Form):
    precipitation = forms.BooleanField(label=ugettext_lazy('Precipitation'),)   
    startdate = forms.DateField(widget = widgets.AdminDateWidget, label=ugettext_lazy('Start Date'),)
    enddate = forms.DateField(widget = widgets.AdminDateWidget, label=ugettext_lazy('End Date'),)

In my views.py I have one view for the form and a separate view for collecting the data via GET. This is very simplified version of the second view.py:

def results(request):
    if 'q' in request.GET:
    ...
    startdate = request.GET.get('startdate', None)
    enddate = request.GET.get('enddate', None)
    data = MyModel.objects.filter(recorded_on__range = (startdate, enddate))
    ...

My date variables in GET are of the format ‘YYYY-MM-DD’.

The problem is that the query raises this error:

coercing to Unicode: need string or buffer, datetime.date found

What is the best way to deal with my date format to make the query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:19:27+00:00Added an answer on May 29, 2026 at 9:19 pm

    You’ll need to create Date objects from your form data, which is currently a string. The error you’re getting is from django trying to compare those strings to the dates on the models.

    so:

    from datetime import datetime
    
    format = '%d-%m-%Y' # Or whatever your date format is
    st = datetime.strptime(startdate, format)
    ed = datetime.strptime(enddate, format)
    
    data = MyModel.objects.filter(recorded_on__range=(st.date(), ed.date()))
    

    Should point you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True,
i have a Reply class: class Reply(models.Model): reply_to = models.ForeignKey(New) creator = models.ForeignKey(User) reply
I have 2 models: class Team(models.Model): Team model for creating a basic team. image
I have 2 projects - a class library containing an EDM Entity Framework model
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have multiple models like this: class Model1(models.Model): user = models.ForeignKey(User) model1_filed1 = models.CharField()
I have a YML file containing fixtures for a Rails model (Comment) which looks
Lets say I have model inheritance set up in the way defined below. class
I think I don't unterstand django-haystack properly: I have a data model containing several
I have a model which looks like this: class test (db.Model) : tagList =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.