I have a model:
class MyModel(models.Model):
id = models.IntegerField(primary_key=True)
recorded_on = models.DateField()
precipitation = models.FloatField(null=True, blank=True)
in my views I have a query thus:
import datetime
def my_view(request):
...
format = '%Y-%m-%d'
sd = datetime.datetime.strptime(startdate, format)
ed = datetime.datetime.strptime(enddate, format)
queryset = MyModel.objects.filter((recorded_on__range = (sd, ed)))
...
But whenever I try and do anything with the queryset (e.g. json dump, display in template), I get the following error:
coercing to Unicode: need string or buffer, datetime.date found
I know there must be an easy way to deal with this, but I have not yet found it.
Any help would be much appreciated.
EDIT:
An example of data:
+----+-------------+---------------+
| id | recorded_on | precipitation |
+----+-------------+---------------+
| 24 | 1987-07-02 | 20.7 |
| 33 | 1987-07-11 | 0.4 |
+----+-------------+---------------+
recorded_onwith unicode string using+operator? If so, the way to do it isu"a string" + str(obj.recorded_on)– vartec Mar 28 '12 at 15:23>>> list(queryset)which gives the same coercion error. So I guess I need to act directly on the queryset before I can do anything with it. – Darwin Tech Mar 28 '12 at 15:35