This digs through _meta to figure any related fields and tries each
until one works.
class Document(models.Model):
...
def __unicode__(self):
name_map = self._meta._name_map
subs = [x for x in name_map if isinstance(name_map[x][0],
RelatedObject)]
for field in subs:
try:
sub = getattr(self, field, None)
return sub.__unicode__()
except ObjectDoesNotExist:
pass
return u'Document {0}'.format(self.id)
On Sep 8, 9:01 am, Jan Ostrochovsky <[email protected]>
wrote:
> Ugly workaround:
>
> class Document(models.Model):
> pass
> def __unicode__(self):
> if self.accountingdocument:
> return self.accountingdocument.__unicode__()
> else:
> return self.__class__.__name_ + ' ' + self.id_
>
> But I'd prefer solution, where Document class does not know about its
> child classes (AccountingDocument, etc.). Any idea?
>
> On Sep 8, 8:31 am, Jan Ostrochovsky <[email protected]>
> wrote:
>
> > Hello,
>
> > class Document(models.Model):
> > ...
>
> > class AccountingDocument(Document):
> > ...
>
> > How am I able to access methods an attributes of some
> > AccountingDocument instance (e.g. its __unicode__()) from Document
> > instance? How am I able to retrieve child object from its parent?
>
> > I tried to search on the web, but I am not a lot wiser after that...
>
> > Thanks in advance.
>
> > Jano
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---