我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用sqlalchemy_utils.ChoiceType()。
def User(self, Base): class User(Base): TYPES = [ ('admin', 'Admin'), ('regular-user', 'Regular user') ] __tablename__ = 'user' id = sa.Column(sa.Integer, primary_key=True) type = sa.Column(ChoiceType(TYPES)) def __repr__(self): return 'User(%r)' % self.id return User
def test_string_processing(self, session, User): flexmock(ChoiceType).should_receive('_coerce').and_return( u'admin' ) user = User( type=u'admin' ) session.add(user) session.commit() user = session.query(User).first() assert user.type.value == u'Admin'
def test_throws_exception_if_no_choices_given(self): with pytest.raises(ImproperlyConfigured): ChoiceType([])
def test_init_type(self): type_ = ChoiceType([(1, u'something')], impl=sa.Integer) assert type_.impl == sa.Integer
def Order(self, Base, OrderStatus): class Order(Base): __tablename__ = 'order' id_ = sa.Column(sa.Integer, primary_key=True) status = sa.Column( ChoiceType(OrderStatus, impl=sa.Integer()), default=OrderStatus.unpaid, ) def __repr__(self): return 'Order(%r, %r)' % (self.id_, self.status) return Order