vendredi 11 août 2017

Django Serializer Field Optional

Right now I have an input and two checkboxes that will return a variable to python back end. I would like to have the two checkbox with default value false and if they are checked to return true. I think my problem is as I'm using a Serializer, that I don't get a value from the two checkboxes if they are unchecked. If the checkbox are checked my code works and the value is saved in the model. If not I get an error

html

<input type="text" ng-model="var.name">
<input ng-model="var.pem" type="checkbox">
<input ng-model="var.key" type="checkbox">

Serializer

class CreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thing
        fields = (  
                    'name',
                    'pem',
                    'key',
                 )

model

class Thing(models.Model):
        name = models.CharField(max_length=50)
        pem = models.BooleanField(default=False)
        key = models.BooleanField(default=False)

Would it be possible to have an existing value for the Serializer if the checkbox stay unchecked?




Aucun commentaire:

Enregistrer un commentaire