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.

--~--~---------~--~----~------------~-------~--~----~
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