本文整理匯總了Python中wtforms.ext.sqlalchemy.orm.model_form方法的典型用法代碼示例。如果您正苦於以下問題:Python orm.model_form方法的具體用法?Python orm.model_form怎麽用?Python orm.model_form使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類wtforms.ext.sqlalchemy.orm
的用法示例。
在下文中一共展示了orm.model_form方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: model_fields
# 需要導入模塊: from wtforms.ext.sqlalchemy import orm [as 別名]
# 或者: from wtforms.ext.sqlalchemy.orm import model_form [as 別名]
def model_fields(model, db_session=None, only=None, exclude=None,
field_args=None, converter=None, exclude_pk=False,
exclude_fk=False):
"""
Generate a dictionary of fields for a given SQLAlchemy model.
See `model_form` docstring for description of parameters.
"""
mapper = model._sa_class_manager.mapper
converter = converter or ModelConverter()
field_args = field_args or {}
properties = []
for prop in mapper.iterate_properties:
if getattr(prop, 'columns', None):
if exclude_fk and prop.columns[0].foreign_keys:
continue
elif exclude_pk and prop.columns[0].primary_key:
continue
properties.append((prop.key, prop))
# ((p.key, p) for p in mapper.iterate_properties)
if only:
properties = (x for x in properties if x[0] in only)
elif exclude:
properties = (x for x in properties if x[0] not in exclude)
field_dict = {}
for name, prop in properties:
field = converter.convert(
model, mapper, prop,
field_args.get(name), db_session
)
if field is not None:
field_dict[name] = field
return field_dict
示例2: ModelForm
# 需要導入模塊: from wtforms.ext.sqlalchemy import orm [as 別名]
# 或者: from wtforms.ext.sqlalchemy.orm import model_form [as 別名]
def ModelForm(model, db_session, exclude=None):
return model_form(
model=model,
db_session=db_session,
base_class=Form,
exclude_fk=False,
exclude=exclude
)
示例3: model_form
# 需要導入模塊: from wtforms.ext.sqlalchemy import orm [as 別名]
# 或者: from wtforms.ext.sqlalchemy.orm import model_form [as 別名]
def model_form(model, db_session=None, base_class=Form, only=None,
exclude=None, field_args=None, converter=None, exclude_pk=True,
exclude_fk=True, type_name=None):
"""
Create a wtforms Form for a given SQLAlchemy model class::
from wtforms.ext.sqlalchemy.orm import model_form
from myapp.models import User
UserForm = model_form(User)
:param model:
A SQLAlchemy mapped model class.
:param db_session:
An optional SQLAlchemy Session.
:param base_class:
Base form class to extend from. Must be a ``wtforms.Form`` subclass.
:param only:
An optional iterable with the property names that should be included in
the form. Only these properties will have fields.
:param exclude:
An optional iterable with the property names that should be excluded
from the form. All other properties will have fields.
:param field_args:
An optional dictionary of field names mapping to keyword arguments used
to construct each field object.
:param converter:
A converter to generate the fields based on the model properties. If
not set, ``ModelConverter`` is used.
:param exclude_pk:
An optional boolean to force primary key exclusion.
:param exclude_fk:
An optional boolean to force foreign keys exclusion.
:param type_name:
An optional string to set returned type name.
"""
if not hasattr(model, '_sa_class_manager'):
raise TypeError('model must be a sqlalchemy mapped model')
type_name = type_name or str(model.__name__ + 'Form')
field_dict = model_fields(
model, db_session, only, exclude, field_args, converter,
exclude_pk=exclude_pk, exclude_fk=exclude_fk
)
return type(type_name, (base_class, ), field_dict)