Comments (7)
No one guys? Basically I want to be able to carry forward the functionality into all the n-level recursed fields.
from drf-flex-fields.
Hi there, apologies for the delay. Unfortunately, I haven't had a chance to look into this yet. I'm on vacation next week, but should be able to look at this the following week.
from drf-flex-fields.
@rsinger86 if you could just point me in the correct direction, like where in the code to try out what, that would be enough for me as well.
from drf-flex-fields.
I have same the question. Please when you come back from vacations, let us know
from drf-flex-fields.
hi @noodlebreak, I'm not sure from the linked example how you'd like the behavior tweaked.
When you request http://api.mlocal.com/api/v1/categories/?fields=id,value,children
, do you want the serialized items of the children
array to only have id
and value
fields?
If further clarification is necessary, maybe you could provide some more details on expected input (url w/ params) and output (serialized response)?
from drf-flex-fields.
@rsinger86 you can check the url and the response in my first post in the linked issue in drf-dynamic-field's repo.
But the response I need is to be like this:
{
"count": 20,
"next": null,
"previous": null,
"results": [
{
"id": "5914220d-c455-45ff-a135-e9ac7f307dae",
"value": "Fruits & Vegetables",
"children": [
{
"id": "7426afb3-b2b6-4f74-8a25-6e5e5865fb4f",
"value": "Fresh Vegetables",
"image": null,
"children": [],
}
],
"image": "http://api.mlocal.com/categories/35629/fruits-vegetables-41698-1523944087.png"
}
]
}
Basically, all the nested objects also to respect the fields
filter and have only the fields specified.
from drf-flex-fields.
@noodlebreak , @rsinger86 , I have solve with this code, I've overwritten _make_expanded_field_serializer
method, and added new option called depth
:
def _make_expanded_field_serializer(self, name, nested_expands, nested_includes):
"""
Returns an instance of the dynamically created nested serializer.
"""
field_options = self.expandable_fields[name]
serializer_class = field_options[0]
serializer_settings = copy.deepcopy(field_options[1])
depth = (self.depth - 1) if self.depth is not None else None
if depth:
serializer_settings['depth'] = depth
if depth == 0:
serializer_settings['expand'] = []
if name in nested_expands:
if 'expand' in serializer_settings:
serializer_settings['expand'] += nested_expands[name]
else:
serializer_settings['expand'] = nested_expands[name]
if name in nested_includes:
serializer_settings['fields'] = nested_includes[name]
if serializer_settings.get('source') == name:
del serializer_settings['source']
if type(serializer_class) == str:
serializer_class = self._import_serializer_class(serializer_class)
return serializer_class(**serializer_settings)
I define serializers like this:
class ConjuntoSerializer(FlexFieldsModelSerializer):
class Meta:
model = models.Conjunto
fields = (
'id', 'parent', 'descripcion',
'nombre', 'identificador', 'repetible',
'children_set',
)
expandable_fields = {
'parent': ('main.ConjuntoSerializer', {'source': 'parent'}),
'children_set': ('main.ConjuntoSerializer', {
'source': 'children_set', 'many': True, 'expand': 'children_set'
})
}
And I call from this way ConjuntoSerializer(Conjunto.objects.last(), expand=['~all'], depth=1).data
from drf-flex-fields.
Related Issues (20)
- FlexFieldsFilterBackend does not handle "GenericForeignKey" HOT 2
- 0.9.9 not available on Pypi? HOT 1
- Expand Argument Not Respected When Empty HOT 1
- Release 1.0.0 available on PyPI but not listed in GIthub Releases, and not tagged
- is_expanded does not take into account the wildcard "*" (default) or setting "WILDCARD_VALUES"
- maximum depth of expansion (dot notation) / avoid infinite recursion HOT 2
- <module_path_to_serializer_class>.RelatedSerializer? HOT 1
- _get_expandable_fields() returns a list but is used by get_schema_fields() to concatenate str
- Swagger does not work when add FlexFieldsFilterBackend in my view
- Potential Denial of Service HOT 2
- Passing context to serializer with `request` param causes `AttributeError` on `query_params` HOT 6
- support Django 4.2 and drf 3.14??? HOT 3
- Applying distinct to `fields`
- Does "deferred field" imply under-the-hood `.defer()`? HOT 1
- Support for a list of expansions with multiple expand parameters instead of separated by a comma. HOT 1
- Support lazily evaluating expanded serializer class from a function
- Custom EXPAND_PARAM not usable in Serializer options HOT 1
- Enhancement Request: Support default expansion and omission of fields per drf view or action
- Better notation for deferred fields? HOT 1
- OpenAPI support for query parameters HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drf-flex-fields.