backend.database.
BaseModel
(**kwargs)[source]¶Base table class. It includes convenience methods for creating, querying, saving, updating and deleting models.
__repr_props__
= ()¶Set to customize automatic string representation.
For example:
class User(database.Model):
__repr_props__ = ('id', 'email')
email = Column(String)
user = User(id=1, email='foo@bar.com')
print(user) # prints <User id=1 email="foo@bar.com">
get_by
(**kwargs)[source]¶Get one model by keyword arguments.
Parameters: | kwargs – The model attribute values to filter by. |
---|
filter_by
(**kwargs)[source]¶Find models by keyword arguments.
Parameters: | kwargs – The model attribute values to filter by. |
---|
create
(commit=False, **kwargs)[source]¶Create a new model and add it to the database session.
Parameters: |
|
---|
update
(commit=False, **kwargs)[source]¶Update fields on the model.
Parameters: |
|
---|
backend.database.
Model
(**kwargs)[source]¶Base table class that extends backend.database.BaseModel
and
includes a primary key id
field along with automatically
date-stamped created_at
and updated_at
fields.
backend.database.
foreign_key
(model_or_table_name, fk_col=None, primary_key=False, **kwargs)[source]¶Helper method to add a foreign key Column to a model.
For example:
class Post(Model):
category_id = foreign_key('Category')
category = relationship('Category', back_populates='posts')
Is equivalent to:
class Post(Model):
category_id = Column(BigInteger, ForeignKey('category.id'), nullable=False)
category = relationship('Category', back_populates='posts')
Parameters: |
|
---|
backend.database.
attach_events
(*args)[source]¶Class decorator for SQLAlchemy models to attach listeners on class
methods decorated with on()
Usage:
@attach_events
class User(Model):
email = Column(String(50))
@on('email', 'set')
def lowercase_email(self, new_value, old_value, initiating_event):
self.email = new_value.lower()
backend.database.
on
(*args, **listen_kwargs)[source]¶Class method decorator for SQLAlchemy models. Must be used in
conjunction with the attach_events()
class decorator
Usage:
@attach_events
class Post(Model):
uuid = Column(String(36))
post_tags = relationship('PostTag', back_populates='post') # m2m
# instance event (only one positional argument, the event name)
# kwargs are passed on to the sqlalchemy.event.listen function
@on('init', once=True)
def generate_uuid(self, args, kwargs):
self.uuid = str(uuid.uuid4())
# attribute event (two positional args, field name and event name)
@on('post_tags', 'append')
def set_tag_order(self, post_tag, initiating_event):
if not post_tag.order:
post_tag.order = len(self.post_tags) + 1
backend.database.
slugify
(field_name, slug_field_name=None, mutable=False)[source]¶Class decorator to specify a field to slugify. Slugs are immutable by default unless mutable=True is passed.
Usage:
@slugify('title')
def Post(Model):
title = Column(String(100))
slug = Column(String(100))
# pass a second argument to specify the slug attribute field:
@slugify('title', 'title_slug')
def Post(Model)
title = Column(String(100))
title_slug = Column(String(100))
# optionally set mutable to True for a slug that changes every time
# the slugified field changes:
@slugify('title', mutable=True)
def Post(Model):
title = Column(String(100))
slug = Column(String(100))