> -----Original Message----- > From: django-users@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of M.Ganesh > Sent: 06 June 2008 14:19 > To: django-users@googlegroups.com > Subject: Help - m2m field in my model > > > Hi, > > Excuse me for the dumb question. > > My model : > > class phonenumber(models.Model): > location_type = models.ForeignKey(location_type) > location_description = models.CharField(max_length=50, blank=True) > > def __unicode__(self): > if self.location_description: > retval = self.location_description + ' - ' > else: > retval = self.location_type + ' - ' > return retval > > class Meta: > app_label = 'contacts' > > > I get an error saying : > TypeError at /contacts/phonenumber/ > unsupported operand type(s) for +: 'location_type' and 'str' > > How do I get the location_type __unicode__ for the fk?
self.location_type is a ForeignKey object (linked to your location_type model subclass) not a string, so you either need to do self.location_type.foo where foo is an attribute of the location_type class with type str, or define a __str__ method of the location_type object. If you call your phonenumber class PhoneNumber, and your location_type class LocationType, it will help make it clearer what is an object and what is a variable. In particular this line will be clearer: location_type = models.ForeignKey(LocationType) (location_type is variable, LocationType is class name) Hope this is not confusing (/ wrong!!). Em --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---