1

I have a SQLAlchemy model with custom type ChoiceType which comes from sqlalchemy_utils library.

class Recipient(Base, BaseMixin):
    first_name = Column(String())
    last_name = Column(String())
    social_network = Column(ChoiceType(SOCIAL_NETWOKRS))

Where SOCIAL_NETWOKRS are SOCIAL_NETWOKRS = [ ('vk', 'Vkontakte'), ('fb', 'Facebook'), ('youtube', 'Youtube'), ]

I got next error when going into admin panel for edit my model:

NotImplementedError: Not able to derive a colander type from sqlalchemy type: ChoiceType(length=255) Please explicitly provide a colander `typ` for the "social_network" Column.

How can I get around the restriction with saving autogeneration of the administrative panel?

eirenikos
  • 2,296
  • 25
  • 25

1 Answers1

1

I move from sqlalchemy_utils and add direct validation from a Colander.

Next snippet works as expected:

class Account(BaseMixin, Base):
    social_network = Column(String(), info={'colanderalchemy': {
        'typ': colander.String(),
        'widget': deform.widget.SelectWidget(values=SOCIAL_NETWOKRS),
    }})
eirenikos
  • 2,296
  • 25
  • 25
  • I have only used PSQL enums for this kind of use cases, so it might be that Choice alternative does not work. But if you want to see an example here is one: https://github.com/websauna/websauna.wallet/blob/master/websauna/wallet/models/account.py#L165 – Mikko Ohtamaa Jan 31 '17 at 13:28
  • Also you can explicitly define a colander.SchemaNode() in ìncludes` list of the admin form, see `adminviews.py` here https://websauna.org/docs/narrative/crud/admin.html#edit-view-example – Mikko Ohtamaa Jan 31 '17 at 13:29
  • It's helpful! Thank you! – eirenikos Jan 31 '17 at 13:31