-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for nested metadata representation of nested serializers #2915
Comments
So, right place to start would be: provide simplest possible example, and show:
|
(I realize that there's a similar description here, but it'd be good to have an example case laid out) |
Sure. Consider the following serializers: class NestedSerializer(serializers.Serializer):
some_text = serializers.CharField(max_length=100)
another_text = serializers.CharField(max_length=100)
class RootSerializer(serializers.Serializer):
nest = NestedSerializer()
some_note = serializers.CharField(max_length=100)
another_note = serializers.CharField(max_length=100) And a view like this: class RootList(generics.ListCreateAPIView):
queryset = Root.objects.all()
serializer_class = RootSerializer
metadata_class = SimpleMetadata As it is now, the representation of metadata in an OPTIONS request looks like this: {
"name": "Root List",
"description": "List existing 'Root' items, or create a new item.",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"nest": {
"type": "field",
"required": true,
"read_only": false,
"label": "nest"
},
"some_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "some note",
"max_length": 100
},
"another_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "another note",
"max_length": 100
}
}
}
} With my suggestion, the representation of metadata in an OPTIONS request will look like this: {
"name": "Root List",
"description": "List existing 'Root' items, or create a new item.",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"nest": {
"type": "serializer",
"required": true,
"read_only": false,
"label": "nest",
"fields": {
"some_text": {
"type": "string",
"required": true,
"read_only": false,
"label": "some text",
"max_length": 100
},
"another_text": {
"type": "string",
"required": true,
"read_only": false,
"label": "another text",
"max_length": 100
}
}
},
"some_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "some note",
"max_length": 100
},
"another_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "another note",
"max_length": 100
}
}
}
} As you can see, with my suggestion, metadata for the fields of the 'nest' field is included in the representation. So, you are getting the whole picture. |
Thought: Also, how would we handle serializers with many = True? |
I agree.
In that case, I suggest the metadata object be encapsulated in a list. For example, with the following serializers: class NestedSerializer(serializers.Serializer):
some_text = serializers.CharField(max_length=100)
another_text = serializers.CharField(max_length=100)
class RootSerializer(serializers.Serializer):
nests = NestedSerializer(many=True)
some_note = serializers.CharField(max_length=100)
another_note = serializers.CharField(max_length=100) the root serializer would have its metadata represented like this: {
"name": "Root List",
"description": "List existing 'Root' items, or create a new item.",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"actions": {
"POST": {
"nests": [
{
"type": "object",
"required": true,
"read_only": false,
"label": "nest",
"fields": {
"some_text": {
"type": "string",
"required": true,
"read_only": false,
"label": "some text",
"max_length": 100
},
"another_text": {
"type": "string",
"required": true,
"read_only": false,
"label": "another text",
"max_length": 100
}
}
}
],
"some_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "some note",
"max_length": 100
},
"another_note": {
"type": "string",
"required": true,
"read_only": false,
"label": "another note",
"max_length": 100
}
}
}
} What do you think? |
Hi, I'm interested in this topic. In case a child serializer is many=true, we should add a flag to the output, like this:
|
We now support nested lists/many #3104 (but not yet nested serializers/mappings) |
I just ran into this issue as well. |
push serialzer metadata into field info for field re encode#2915
Accepting this issue. |
Now resolved. Eg:
And this:
Will include this in the OPTIONS output
|
Awesome! Thanks! : ) |
Currently, the representation of metadata done with SimpleMetadata class for a serializer that includes nested serializers does not include metadata for the inner fields of the nested serializers. To support the inclusion of nested serializer's inner fields metadata in the representation, I wrote pull request #2911, which works like this:
The above will work for nested serializers of arbitrary depth.
However, @tomchristie suggested that a discussion is first opened before going that way, as there may be other ways to represent this. So, let's start this discussion.
The text was updated successfully, but these errors were encountered: