On Sat, May 23, 2009 at 11:51 AM, Carlos A. Carnero Delgado <
carloscarn...@gmail.com> wrote:

>
> Hello,
>
> I'm having a little problem that's making my head hurt. I'm getting an
> unintended date to string conversion in my application.
>
>  >>> import django
>  >>> django.VERSION
>  (1, 1, 0, 'beta', 1)
>  >>> import djtest.settings
>  >>> djtest.settings.DATABASE_ENGINE
>  'sqlite3'
>
> Obviously, djtest is the name of the project, which I made from
> scratch just to replicate what's happening to me. OK, the models:
>
>  class MediaFile(models.Model):
>      title = models.CharField(max_length=1024)
>      added_on = models.DateTimeField(auto_now_add=True, db_index=True)
>      is_published = models.BooleanField(default=False)
>
>      def __unicode__(self):
>          return '%d: %s' % (self.id, self.title)
>
>  class Hits(models.Model):
>      media_file = models.ForeignKey(MediaFile)
>      day = models.DateField(auto_now_add=True, db_index=True)
>      hits = models.PositiveIntegerField(default=0)
>
>      def __unicode__(self):
>          return '%d hits for %s on %s' % (self.media_file.title,
>                                           self.hits,
>                                           strftime('%Y%m%d',
>                                                    self.day.timetuple()))
>
> The model is simple: there's a many-to-one relationship from Hits to
> MediaFile, each record of Hits contains the number of hits (whatever
> that is) that a MediaFile had on a given day.
>
> After adding some sample data, this is what I get:
>
>  >>> media_file = MediaFile.objects.all()[0]
>  >>> media_file.title
>  u'This is a media file'
>  >>> media_file.added_on
>  datetime.datetime(2009, 5, 23, 12, 6, 14, 484000)
>  >>> media_file.hits
>  Traceback (most recent call last):
>    File "<pyshell#71>", line 1, in <module>
>      media_file.hits
>  AttributeError: 'MediaFile' object has no attribute 'hits'
>
> which is absolutely correct. Note that added_on is a datetime.datetime
> instance. Now, if I apply an aggregation:
>
>  >>> media_file = MediaFile.objects.annotate(hits=Sum('hits__hits'))[0]
>  >>> media_file.title
>  u'This is a media file'
>  >>> media_file.added_on
>  u'2009-05-23 12:06:14.484000'
>  >>> media_file.hits
>  11
>
> Notice how, after the aggregation and annotation, the added_on field
> is now a Unicode string.
>
> What's I'm doing wrong?
>
> Best regards,
> Carlos.
>
> >
>
There was a bug like this seen when aggregates were first added that no one
was ever able to figure out.  If you search django-developers you'll see the
discussions of this, it was only ever seen on Windows with SQLite, is that
what you're using?

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to