Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way in Django Rest framework serializer for ignoring case in choice field?

class MySerializer(serializers.Serializer):
  contract = fields.ChoiceField(choices=(
    ('no', 'no'),
    ('yes', 'yes'),
  ))

So here my input can be one of the following. no, No,Yes,yes

for these do I need to add 2 more entry for Capital one?

contract = fields.ChoiceField(choices=(
    ('no', 'no'),
    ('yes', 'yes'),
    ('No', 'no'),
    ('Yes', 'yes'),

  ))

or is there any way by which we can ignore the case?

like image 362
Sanch Avatar asked Oct 07 '15 09:10

Sanch


People also ask

How do you make a serializer optional in Django?

Set to false if this field is not required to be present during deserialization. Setting this to False also allows the object attribute or dictionary key to be omitted from output when serializing the instance. If the key is not present it will simply not be included in the output representation. Defaults to True .

What is difference between serializer and ModelSerializer?

The ModelSerializer class is the same as a regular Serializer class, except that: It will automatically generate a set of fields for you, based on the model. It will automatically generate validators for the serializer, such as unique_together validators.

What does serializer Is_valid do?

is_valid perform validation of input data and confirm that this data contain all required fields and all fields have correct types. If validation process succeded is_valid set validated_data dictionary which is used for creation or updating data in DB.


1 Answers

http://www.django-rest-framework.org/api-guide/fields/#choicefield

If you want to leave it to user, you might have to think of more options like "yes", "Yes", "YES" instead of just "yes, "Yes"

I prefer you convert them to lower and set it on the field by using .lower(), so that you will always get lowercase letters as input

like image 69
be_good_do_good Avatar answered Oct 03 '22 07:10

be_good_do_good