I've got an issue with Django where I am attempting to insert a new row into a MySQL database without providing NULLs for unspecified data.
For example, consider the following:
class MyModel(models.Model):
class Meta:
db_table = 'model_table'
managed = False
a = models.AutoField(primary_key=True)
b = models.CharField(max_length=255)
c = models.CharField(max_length=255, blank=True)
When providing the following data to the Serializer for a save():
data = list()
data['b'] = 'Some Text'
serializer = serializers.MyModelSerializer(data=data)
serializer.save()
The resulting SQL generated has NULL for every unspecified field, in this case 'c'. The table in the database does not allow NULL but has default values in place to handle unspecified data.
Is it possible to override this Serializer functionality to allow fields to be entirely omitted when attempting to insert, or will I have to create a new model with those fields omitted and have a 'creation' model for this specific case that does not have any awareness of these fields?
Note: the Serializer only has the field 'b' in its fields
variable, so it has no immediate awareness of the 'c' column.
Edit: to try and clarify a little, this is the SQL that is generated, which due to nulls not being allowed in the column, is failing. 'c' does have default values however.
INSERT INTO `model_table` (`b`, `c`) VALUES ('Some Text', NULL)
It's fine for the AutoField of 'a' the PK, this is omitted by default, but 'c' is replaced with a NULL due to no value being provided in the data provided to the serializer, instead of being omitted form the SQL entirely. As a workaround I've duplicated the model and removed the fields I don't need from it just to get it working, and the SQL generated is valid due to the model itself having no awareness of the columns, but this does not seem like the best way to do this. The following is what I want this model and serializer combination to output
INSERT INTO `model_table` (`b`) VALUES ('Some Text')
so that 'c' is provided the default column value provided by MySQL itself. The model and serializer attmepting to insert NULL when I have provided not provided any data to the serializer is the problem.