On Tuesday, 21 June 2011 15:47:32 UTC+1, Jeff Blaine wrote: > > Okay, here's the problem. > > >>> f = dev._meta.get_field('distro') > >>> f.value_from_object(dev) > 2L > > value_from_object is defined as: > > ======================================================== > def value_from_object(self, obj): > "Returns the value of this field in the given model instance." > return getattr(obj, self.attname) > ======================================================== > > >>> f.attname > 'distro_id' > >>> > > As others guessed, value_from_object() is returning the pk ID in this > case. > > Model "Distro" is the only model of mine showing this behavior, as it is > the > only foreign key "target" model I have defined without named primary key. > > From what I can tell, value_from_object() is actually mis-named, and should > be named pkvalue_from_object(), no? Or is there some case where > self.attname > for a field is *not* set to the primary key attribute name? > > Any ideas? I really want (concept) > "myfield.unicode_representation_of_object(myobject)" >
But why do you need to get that value via the *field* object? The usual way to do it is simply to access the attribute directly on the model instance: unicode(dev.distro) or, if you have the field name as a string as you imply originally, use `getattr`: unicode(getattr(dev, 'distro')) -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Ch9SdssM6FoJ. 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.