How to remove auto-generated fields from DRF - Swagger UI

Viewed 1070

I've a model where few fields are auto-generated. How do I hide those fields from the Swagger UI during POST request? Following is the example:-

class ModelX(models.Model):
    a = models.CharField()
    b = models.CharField()
    c = models.CharField()
    d = models.CharField()

Below is my serialzer:-

class Serializerx(serializers.Serializer):
    class Meta:
        model = ModelX
        fields = '__all__

In the above model, fields b and d are auto generated from my code, which means these fields are not required as an input from the user.

If I add b and d as read-only fields, then I wont be able to create an object with these values.

How do I hide some attributes from the payload request.? Somewhat similar to this

2 Answers

In the Meta class you can specify which fields you don't want to display:

class Serializerx(serializers.Serializer):
    class Meta:
        model = ModelX
        exclude = ('a','c','or_whatever_field_you_want_to_exclude')

Just specify which fields you want to be displayed.

class Serializerx(serializers.Serializer):
class Meta:
    model = ModelX
    fields = ['a','c',]
Related