wuttjamaican.db.model.base
¶
Base Models
- class wuttjamaican.db.model.base.Base¶
This is the base class for all data models in the app database. You should inherit from this class when defining custom models.
This class inherits from
WuttaModelBase
.
- class wuttjamaican.db.model.base.Person(**kwargs)[source]¶
Represents a person.
The use for this table in the base framework, is to associate with a
User
to provide first and last name etc. (However a user does not have to be associated with any person.)But this table could also be used as a basis for a Customer or Employee relationship etc.
- first_name¶
The person’s first name.
- full_name¶
Full name for the person. Note that this is required.
- last_name¶
The person’s last name.
- middle_name¶
The person’s middle name or initial.
- property user¶
Reference to the “first”
User
account for the person, orNone
.Warning
Note that the database schema supports multiple users per person, but this property logic ignores that and will only ever return “one or none”. That might be fine in 99% of cases, but if multiple accounts exist for a person, the one returned is indeterminate.
See
users
to access the full list.
- class wuttjamaican.db.model.base.Setting(**kwargs)[source]¶
Represents a config setting.
- name¶
Unique name for the setting.
- value¶
String value for the setting.
- class wuttjamaican.db.model.base.WuttaModelBase[source]¶
Base class for data models, from which
Base
inherits.Custom models should inherit from
Base
instead of this class.- classmethod make_proxy(main_class, extension, name, proxy_name=None)[source]¶
Convenience method to declare an “association proxy” for the main class, per the params.
For more info see Association Proxy.
- Parameters:
main_class – Reference to the “parent” model class, upon which the proxy will be defined.
extension – Attribute name on the main class, which references the extension record.
name – Attribute name on the extension class, which provides the proxied value.
proxy_name – Optional attribute name on the main class, which will reference the proxy. If not specified,
name
will be used.
As a simple example consider this model, which extends the
User
class. In particular note the last line which is what we’re documenting here:import sqlalchemy as sa from sqlalchemy import orm from wuttjamaican.db import model class PoserUser(model.Base): """ Poser extension for User """ __tablename__ = 'poser_user' uuid = model.uuid_column(sa.ForeignKey('user.uuid'), default=None) user = orm.relationship( model.User, doc="Reference to the main User record.", backref=orm.backref( '_poser', uselist=False, cascade='all, delete-orphan', doc="Reference to the Poser extension record.")) favorite_color = sa.Column(sa.String(length=100), nullable=False, doc=""" User's favorite color. """) def __str__(self): return str(self.user) # nb. this is the method call PoserUser.make_proxy(model.User, '_poser', 'favorite_color')
That code defines a
PoserUser
model but also defines afavorite_color
attribute on the mainUser
class, such that it can be used normally:user = model.User(username='barney', favorite_color='green') session.add(user) user = session.query(model.User).filter_by(username='bambam').one() print(user.favorite_color)