class TeacherSchema(SQLAlchemyAutoSchema): class Meta: model = models.Teacher load_instance = True sqla_session = session current_school = Related(["id", "name"])
class SchoolSchema(ModelSchema): class Meta: model = models.School sqla_session = session students = RelatedList(Related(attribute="students"), attribute="students")
def _property2relationship(self, prop, **kwargs): # The "name" is actually the name of the related model, NOT the name # of field cls = self._related_entity(prop) name = to_schema_name(cls.__name__) field_kwargs = self._get_field_kwargs_for_property(prop) field_kwargs.update(**kwargs) this_table = prop.parent.tables[0] if not allow_nest(this_table.name, prop.target.name): # Fields that are not allowed to be nested if prop.uselist: return RelatedList(Related, **field_kwargs) else: return Related(**field_kwargs) if prop.target.schema == "enum": # special carve-out for enums represented as foreign keys # (these should be stored in the 'enum' schema): return Enum(**field_kwargs) # Ignore fields that reference parent models in a nesting relationship exclude = [] for p in cls.__mapper__.relationships: if self._should_exclude_field(p): # Fields that are already excluded do not need to be excluded again. continue id_ = self._get_field_name(p) if p.mapper.entity == prop.parent.entity: exclude.append(id_) return SmartNested(name, many=prop.uselist, exclude=exclude, **field_kwargs)
class TeacherSchema(SQLAlchemyAutoSchema): class Meta: model = models.Teacher load_instance = True sqla_session = session strict = True # marshmallow 2 compat current_school = Related(["id", "name"])
class TransactionSchema(schemas.ModelSchema): created_at = field_for(models.Transaction, 'created_at', dump_only=True) detection_object = Nested(DetectionObjectSchema, dump_only=True) detection_object_id = Related( column='id', attribute='detection_object', load_only=True, ) class Meta: model = models.Transaction sqla_session = db.session fields = ( 'id', 'created_at', 'amount', 'type', 'detection_object', 'detection_object_id', )
class StudentSchema(ModelSchema): class Meta: model = models.Student sqla_session = session current_school = Related(column='name')
class AnekSchema(ma.ModelSchema): class Meta: model = Anek dump_only = ('id', 'created_at') source = Related(column=('name', 'url'))