Checklist
Steps to reproduce
I found that when I use ModelSerializer, if I set a field in ModelSerializer which is duplicate in model. Then ModelSerializer 's exclude attribute won't work well. It still return the exclude fields.
I traced down the code, and found that it failed because ModelSerializer.get_field_names exclude fields by using list. Since I have set the field in the ModelSerializer, there are two fields name in the list returned by ModelSerializer.get_default_field_names.
django model I except to have
class Car(models.Model):
name = models.CharField(max_length=128)
username = models.CharField(max_length=128)
serializar I have
class VersionModelSerializer(serializers.ModelSerializer):
username = fields.IntegerField(validators=my_validator)
class Meta:
model = models.Car
exclude = ('username')
Expected behavior
Excepted to return data like
Actual behavior
but username exists in the response like
{
"name": "car_name",
"username": "username"
}
according to the reason I explain above, I think ModelSerializer.get_field_names should use set instead of list to exclude fields.
Checklist
masterbranch of Django REST framework.Steps to reproduce
I found that when I use ModelSerializer, if I set a field in ModelSerializer which is duplicate in model. Then ModelSerializer 's exclude attribute won't work well. It still return the exclude fields.
I traced down the code, and found that it failed because
ModelSerializer.get_field_namesexclude fields by using list. Since I have set the field in the ModelSerializer, there are two fields name in the list returned byModelSerializer.get_default_field_names.django model I except to have
serializar I have
Expected behavior
Excepted to return data like
Actual behavior
but username exists in the response like
according to the reason I explain above, I think
ModelSerializer.get_field_namesshould usesetinstead oflistto exclude fields.