On Wed, Apr 2, 2008 at 11:41 AM, Josh <[EMAIL PROTECTED]> wrote: > > I've created a sort of modified tagging system for the site I'm > working on. Because of the nature of the 'tags' I had to diverge a > little form the standard and my models look like this: > > class Word(models.Model): > word = models.CharField(max_length=30) > slug = models.SlugField(prepopulate_from=("word",)) > alternate_spelling = models.CharField(max_length=30, blank=True) > pronunciation = models.CharField(max_length=30, blank=True) > pronunciation_example = > models.FileField(upload_to="pronunciations", > blank=True) > notes = models.TextField(blank=True) > categories = models.ManyToManyField("FoodWordsCategory", > blank=True) > > def __unicode__(self): > return self.word > > def save(self): > super(Word, self).save() > foodwords.scan_for_word(self) > > class Admin: > ordering = ('word',) > search_fields = ['word', 'alternate_spelling',] > > class WordedItem(models.Model): > word = models.ForeignKey(Word) > content_type = models.ForeignKey(ContentType) > object_id = models.PositiveIntegerField() > content_object = generic.GenericForeignKey('content_type', > 'object_id') > > def __unicode__(self): > return self.word
This should be return __unicode__(self.word) > > This seems like it should be all well and good, and for the most part > it is. But I'm running into some strange issues that appear to be > related to the fact that WordedItem.word is a ForeignKey. For example: > > >>> relations = word.wordeditem_set.all() > >>> relations > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/home/bostonchefs/lib/python2.5/django/db/models/query.py", > line 108, in __repr__ > return repr(self._get_data()) > File "/home/bostonchefs/lib/python2.5/django/db/models/base.py", > line 125, in __repr__ > return smart_str(u'<%s: %s>' % (self.__class__.__name__, > unicode(self))) > TypeError: coercing to Unicode: need string or buffer, Word found > >>> > > Obviously the problem here is that a Word object is being returned > instead of a unicode string, and I can fix that by simple chaging the > def __unicode__(self) to return a simple string (return "string" is > fine for now). However even when I do that I'm still encountering what > appears to be the same problem elsewhere. If I create a Word object, > thanks to the custom save() method I wrote it automatically associates > itself with the appropriate objects (it does a search on the > appropriate database fields, &c). So essentially any time you create a > Word object you can be sure it's going to be associated with some > WordedItem objects. The problem comes in if you then try and delete > that Word object from the admin interface. When I do so, I get > basically the same error as above: > > TypeError at /admin/words/word/278/delete/ > coercing to Unicode: need string or buffer, Word found > Since this is the error you haven't figured out yet, this is the one that really needs a full traceback in order for people to help figure out what is going on. > This happens even after I change __unicode__(self) to return "string". > But, if I try and delete the Word object from the command line it > successfully deletes both the Word and the related WordedItems. Does > anyone have any idea what might be going on here? > Not yet. Karen --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---