Skip to content

Conversation

@Ernest0x
Copy link
Contributor

@Ernest0x Ernest0x commented May 5, 2015

With this commit, nested serializer fields in the representation done by SimpleMetadata class are getting a type of 'serializer' and a 'fields' attribute containing metadata for their inner fields.

…data

With this commit, nested serializer fields in the representation done by SimpleMetadata class are getting a type of 'serializer' and a 'fields' attribute containing metadata for their inner fields.
@xordoquy
Copy link
Contributor

xordoquy commented May 5, 2015

I don't really get what this change is about. Mind explaining a bit more why this is required and possible add a test case to illustrate it ?

@Ernest0x
Copy link
Contributor Author

Ernest0x commented May 6, 2015

@xordoquy of course, here is an example:

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)

Now you write a view like this:

class RootList(generics.ListCreateAPIView):
    queryset = Root.objects.all()
    serializer_class = RootSerializer
    metadata_class = SimpleMetadata

With this commit, 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, the "nest" field has a "fields" attribute that expands the inner fields of the nested serializer.

Use BaseSerializer instead of Serializer to also catch instances of BaseSerializer.
@lovelydinosaur
Copy link
Contributor

The motivation here is reasonable enough, but let's not start with a pull request until we've discussed through in an issue...

  • What the current style looks like.
  • What alternative styles we might use instead.

Happy to accept this as an issue, but I expect the answer to the second of those points might be slightly different to what you're presenting in this pull request, so best to start with a conversation, rather than a code change. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants