Lets consider an example. Here is the model setup we are going to use for this example: from django.db import models class Bike ( models . Model ): COLOR_OPTIONS = (( 'yellow' , 'Yellow' ), ( 'red' , 'Red' ), ( 'black' , 'Black' )) color = models . CharField ( max_length = 255 , null = True , blank = True , choices = COLOR_OPTIONS ) size = models . DecimalField ( max_digits = 4 , decimal_places = 2 , null = True , blank = True ) And this is the serializer I'm are going to test: from rest_framework import serializers from bikes.models import Bike class BikeSerializer ( serializers . ModelSerializer ): COLOR_OPTIONS = ( 'yellow' , 'black' ) color = serializers . ChoiceField ( choices = COLOR_OPTIONS ) size = serializers . FloatField ( min_value = 30.0 , max_value = 60.0 ) class Meta : model = B
Hi, I’m Deepak Dubey. The founder of this blog. I’m a Full-stack Python Developer | OpenedX professional and part-time blogger. I’m here to write the blogs related to technology stuff like Python, Django , Open-edX, Data Science, machine Learning and many more.