[EMAIL PROTECTED] wrote: > I apologize in advance if this is a stupid question - I'm new to > Django, and a little over my head (but loving it!) > > I'm trying to return a field from a foreign key relationship as part of > my __str__ method. When I do, I get this output in the admin interface: > > Product Name - <django.db.models.fields.related.ManyRelatedManager > object at 0x014F5C30> > > What am I doing wrong? > > Code: > > class Product(models.Model): > name = models.CharField(maxlength=100) > category = models.ManyToManyField(Category) > ... > def __str__(self): > return '%s - %s' % (self.name, self.category) > > class Category(models.Model): > name = models.CharField(maxlength=100) > def __str__(self): > return self.name
You have a ManyToManyField, not ForeignKey, from Product to Category. Which means that "product.category" is not a single object and hence can't be represented as a single category name. Is this ManyToMany intentional? If not, change it to ForeignKey and your __str__ will look right. If yes, you can construct a list of categories in your __str__: def __str__(self): return '%s (%s)' % (self.name, ', '.join(self.category.all())) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---