On Tue, 2010-07-06 at 22:52 +0200, Diez B. Roggisch wrote:
> Am 06.07.2010 um 16:17 schrieb Paul Johnston:
>
> > Hi,
> >
> >> Erm, why don't you use a Unicode-column in the first place?
> >
> > Well, I probably should, but the point of what I'm asking is
> > automatically define the method to convert a class instance to a
> > unicode object.
>
> You mean you want a declaration for one field that says "this field is
> the __unicode__"-representation? I don't think that would make much
> sense. What is the behavior for several such columns having that
> attribute? How is coercion/decoding handled, which btw your example
> lacks?
>
> I'd go for a custom base-class + a class-attribute, like this
>
> class UnicodeEntity(Entity):
>
> UNICODE_COL = None
>
>
> def __unicode__(self):
> return getattr(self, self.UNICODE_COL)
I 100% agree with Diez and I'll just add that abstract base classes are
also quite convenient for that:
class UnicodeEntity(Entity):
using_options(abstract=True)
UNICODE_COL = None
def __unicode__(self):
return getattr(self, self.UNICODE_COL)
class A(UnicodeEntity):
UNICODE_COL = 'name'
name = Field(Unicode(20))
Gaëtan.
--
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en.