bag.sqlalchemy.tricks module¶
Functions that help define SQLAlchemy models.
- class bag.sqlalchemy.tricks.AddressBase[source]¶
Bases:
objectBase class for addresses.
In subclasses you can just define
__tablename__,id, the foreign key, and maybe indexes.- city = Column('city', Unicode(length=80), table=None, default=ColumnDefault(''))¶
- country_code = Column('country_code', Unicode(length=2), table=None, default=ColumnDefault(''))¶
- district = Column('district', Unicode(length=80), table=None, default=ColumnDefault(''))¶
- postal_code = Column('postal_code', Unicode(length=16), table=None, default=ColumnDefault(''))¶
- province = Column('province', Unicode(length=40), table=None, default=ColumnDefault(''))¶
- street = Column('street', Unicode(length=160), table=None, default=ColumnDefault(''))¶
- class bag.sqlalchemy.tricks.EmailParts[source]¶
Bases:
objectMixin class that stores an email address in 2 columns.
One column contains the local part, another contains the domain. This makes it easy to find emails from the same domain.
Typical usage:
class Customer(SABase, EmailParts): __table_args__ = (UniqueConstraint('email_local', 'email_domain', name='customer_email_key'), {})
- email¶
Get or set the entire email, in Python or in the RDBMS.
- email_domain = Column('email_domain', Unicode(length=255), table=None, nullable=False)¶
- email_local = Column('email_local', Unicode(length=160), table=None, nullable=False)¶
- gravatar_image(default='mm', size=80, cacheable=True)[source]¶
Return the URL for the gravatar image for this email address.
- Return type
str
- set_email¶
Get or set the entire email, in Python or in the RDBMS.
- class bag.sqlalchemy.tricks.ID[source]¶
Bases:
objectMixin class that includes a primary key column “id”.
- id = Column(None, Integer(), table=None, primary_key=True, nullable=False)¶
- class bag.sqlalchemy.tricks.MinimalBase[source]¶
Bases:
objectDeclarative base class that auto-generates __tablename__.
- clone(values=None, pk='id', sas=None)[source]¶
Return a clone of this model.
Optionally update some of its
values. Optionally add the clone to thesassession. The name of the primary key column should be given aspk.Although in general model methods should not use the session, the recursive nature of this one seems to require it.
- class bag.sqlalchemy.tricks.Names[source]¶
Bases:
objectMixin class that includes 2 ways to handle a person’s names.
- property display_name¶
- property formal_name¶
- full_name = Column(None, Unicode(length=120), table=None, nullable=False)¶
- short_name = Column(None, Unicode(length=16), table=None, nullable=False)¶
- class bag.sqlalchemy.tricks.SubtransactionTrick(engine, sessionmaker)[source]¶
Bases:
objectEncloses your code in a subtransaction. Good for writing tests.
Usage:
trick = SubtransactionTrick(my_engine, sessionmaker) # Be sure to use the session provided as the ``sas`` variable: my_session = trick.sas # Finally, call ``close()`` to roll back the changes: trick.close()
- bag.sqlalchemy.tricks.col(attrib)[source]¶
Return the column that stores an
attribof a model.Given a sqlalchemy.orm.attributes.InstrumentedAttribute (type of the attributes of model classes), return the corresponding column. E.g.:
col(User.email)
- bag.sqlalchemy.tricks.commit_session_or_transaction(sas)[source]¶
Not sure if using the transaction package or not? No problem.
- Return type
None
- bag.sqlalchemy.tricks.fk(attrib, nullable=False, index=True, primary_key=False, doc=None, ondelete='CASCADE')[source]¶
Return a ForeignKey column while automatically setting the type.
- bag.sqlalchemy.tricks.fk_rel(cls, attrib='id', nullable=False, index=True, primary_key=False, doc=None, ondelete='CASCADE', backref=None, order_by=None, lazy='select')[source]¶
Return a ForeignKey column and a relationship.
Automatically sets the type of the foreign key.
Usage:
# A relationship in an Address model pointing to a parent Person: person_id, person = fk_rel(Person, nullable=False, index=True, backref='addresses', ondelete='CASCADE')
A backref is created only if you provide its name in the argument.
nullableandindexare usually ommited, because these are the default values and they are good.ondeleteis “CASCADE” by default, but you can set it to “SET NULL”, or None which translates to “NO ACTION” (less interesting). If provided,order_byis used on the backref.To load the backref greedily, use
lazy='joined'as per http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.htmlYou may also pass an
attribwhich is the column name for the foreign key.
- bag.sqlalchemy.tricks.get_col(model, attribute_name)[source]¶
Introspect the SQLAlchemy
model; return the column object.…for
attribute_name. E.g.:get_col(User, 'email')
- bag.sqlalchemy.tricks.get_length(model, field)[source]¶
Return the length of column
fieldof a SQLAlchemymodel.
- bag.sqlalchemy.tricks.is_model_class(val)[source]¶
Return whether the parameter is a SQLAlchemy model class.
- Return type
bool
- bag.sqlalchemy.tricks.many_to_many(Model1, Model2, pk1='id', pk2='id', metadata=None, backref=None)[source]¶
Easily set up a many-to-many relationship between 2 existing models.
Return an association table and the relationship itself.
Usage:
- customer_user, Customer.users = many_to_many(Customer, User,
pk2=’__id__’)
- bag.sqlalchemy.tricks.model_property_names(cls, whitelist=None, blacklist=None, include_relationships=True)[source]¶
Return the property names in the passed class, maybe filtered.
- bag.sqlalchemy.tricks.models_and_tables_in(arg)[source]¶
Return 2 lists containing the model classes and tables in
arg.argmay be a resource spec, a module or a dictionary:models, tables = models_and_tables_in(globals())
- Return type
Tuple[List,List]
- bag.sqlalchemy.tricks.now_column(nullable=False, **k)[source]¶
Return a DateTime column that defaults to utcnow.
- Return type
Column