I have a problem with a date (DateBaja) because in this case it is a field that can be blank.
In the frontend I use a datepicker for that date field, the format of the datepicker is specified to dateFormat: 'dd/mm/yy',
But the format in which the serializer returns the data is yyyy-mm-dd
Until now I had been using dates in this format yyyy-mm-dd and I had not been given this case.
To change this I have specified in the serializer how it should be the format of the dates.
The problem is that I can not empty the field, that is, if I put 01/01/2018 and then I want to delete this date from the input, it tells me that the empty field does not match the format.
I include part of the model and the serializer
Model:
class Contrato(models.Model):
FechaContrato = models.DateField(null = True, blank = False)
FechaRenovacion = models.DateField(null = True, blank = False)
Renovado = models.BooleanField(default = False, blank = True)
Baja = models.BooleanField(default = False, blank = True)
FechaBaja = models.DateField(null = True, blank = True)
Serializer:
class ContratoEditSerializer(serializers.ModelSerializer):
FechaContrato = serializers.DateField(format="%d/%m/%Y", input_formats=['%d/%m/%Y', 'iso-8601'])
FechaRenovacion = serializers.DateField(format="%d/%m/%Y", input_formats=['%d/%m/%Y', 'iso-8601'])
FechaBaja = serializers.DateField(format="%d/%m/%Y", input_formats=['%d/%m/%Y','iso-8601'])
class Meta:
model = Contrato
If I comment the line DateBaja of the serializer, and remove the datepicker from that input, it works correctly, that is, it allows me to empty the field when necessary. That if, when specifying a date, I must do it with the default date format in which the rest framework is working YYYY [-MM [-DD]]
What is the best way to approach this?
Thanks for help!