Skip to content

Make input fields not required for DRF Serialize #1079

Closed
@rganeyev

Description

@rganeyev

Is your feature request related to a problem? Please describe.
I have a model and DRF serializer.

class Center(models.Model):
    name = models.CharField(max_length=255) # required
    homepage = models.URLField(null=True, blank=True)
    
  class CenterSerializer(serializers.ModelSerializer):
    class Meta:
        model = Center
        fields = ['name', 'homepage']

I want to create an update mutation for existing object:

  class CenterMutation(SerializerMutation):
    class Meta:
        serializer_class = CenterSerializer
        model_operations = ('update')
        lookup_field = 'id'

As you can see from model, name is required in the model and CenterMutationInput will be as follows:

CenterMutationInput {
  name: String!
  homepage: String
}

In update mutation it should be fully partial, meaning that we can skip name for the input. Is that achievable?

Describe the solution you'd like
I would like to introduce a parameter in meta to make some fields optional, like this:

class CenterMutation(SerializerMutation):
    class Meta:
        serializer_class = CenterSerializer
        model_operations = ('update')
        lookup_field = 'id'
        optional_fields = ('name',) # mark fields optional

Is there any existing solution or alternative to the described approach?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions