Friday, December 14, 2012

Re: Model's optional __unicode__() return values?

The pattern I use is ", ".join ( [ unicode(x) for x in ( thing_a, thing_b, getattr(obj,"optional_attribute",None ), ...etc... ) where x is not None ] )


On Friday, December 14, 2012 2:49:16 PM UTC-8, mhulse wrote:
Hello,

This is probably a dumb question, but...

What's the best way to handle optional __unicode__() return values?

In this situation, I'm wanting to return the __unicode__ values from
other models.

I've found myself doing a lot of this:

def __unicode__(self):
    return _(u'%s%s%s') % (self.target, (' | ' if self.page_type else
''), (self.page_type if self.page_type else ''))

Which works, but seems clunky.

I've tried:

return _(u'%s') % ' | '.join(filter(None, (self.target, getattr(self,
'page_type', None))))

But that gives me:

Caught TypeError while rendering: sequence item 0: expected string, Target found

Tips would be appreciated. :)

Thank you!

Cheers,
M

--
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/-/GWVx5_ksMnUJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate