class SchoolSchema(ModelSchema):
            class Meta:
                model = models.School
                sqla_session = session

            students = RelatedList(Related(attribute="students"),
                                   attribute="students")
Exemplo n.º 2
0
    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)