|
5 | 5 | from sqlalchemy import and_, or_, not_ |
6 | 6 |
|
7 | 7 | from flask_rest_jsonapi.exceptions import InvalidFilters |
8 | | -from flask_rest_jsonapi.schema import get_relationships, get_model_field |
| 8 | +from flask_rest_jsonapi.schema import get_relationships, get_nested_fields, get_model_field |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def create_filters(model, filter_info, resource): |
@@ -143,26 +143,27 @@ def value(self): |
143 | 143 |
|
144 | 144 | @property |
145 | 145 | def related_model(self): |
146 | | - """Get the related model of a relationship field |
| 146 | + """Get the related model of a related (relationship or nested) field |
147 | 147 |
|
148 | 148 | :return DeclarativeMeta: the related model |
149 | 149 | """ |
150 | | - relationship_field = self.name |
| 150 | + related_field_name = self.name |
151 | 151 |
|
152 | | - if relationship_field not in get_relationships(self.schema): |
153 | | - raise InvalidFilters("{} has no relationship attribute {}".format(self.schema.__name__, relationship_field)) |
| 152 | + related_fields = get_relationships(self.schema) + get_nested_fields(self.schema) |
| 153 | + if related_field_name not in related_fields: |
| 154 | + raise InvalidFilters("{} has no relationship or nested attribute {}".format(self.schema.__name__, related_field_name)) |
154 | 155 |
|
155 | | - return getattr(self.model, get_model_field(self.schema, relationship_field)).property.mapper.class_ |
| 156 | + return getattr(self.model, get_model_field(self.schema, related_field_name)).property.mapper.class_ |
156 | 157 |
|
157 | 158 | @property |
158 | 159 | def related_schema(self): |
159 | | - """Get the related schema of a relationship field |
| 160 | + """Get the related schema of a related (relationship or nested) field |
160 | 161 |
|
161 | 162 | :return Schema: the related schema |
162 | 163 | """ |
163 | | - relationship_field = self.name |
| 164 | + related_field_name = self.name |
| 165 | + related_fields = get_relationships(self.schema) + get_nested_fields(self.schema) |
| 166 | + if related_field_name not in related_fields: |
| 167 | + raise InvalidFilters("{} has no relationship or nested attribute {}".format(self.schema.__name__, related_field_name)) |
164 | 168 |
|
165 | | - if relationship_field not in get_relationships(self.schema): |
166 | | - raise InvalidFilters("{} has no relationship attribute {}".format(self.schema.__name__, relationship_field)) |
167 | | - |
168 | | - return self.schema._declared_fields[relationship_field].schema.__class__ |
| 169 | + return self.schema._declared_fields[related_field_name].schema.__class__ |
0 commit comments