Reputation: 1093
Let's say I have the below structure of Models for a django project with MongoDB Database, using djongo as DB engine.
When I am trying to add a Department record, if I don't provide the salary field which is marked as default=0
in the model declaration, I get a ValidationError
for not supplied fields
class Person(models.Model):
name = models.CharField(max_length=60)
surname = models.CharField(max_length=60)
salary = models.FloatField(default=0)
class Meta:
abstract = True
class Department(models.Model):
name = models.CharField(max_length=60, unique=True, null=False)
persons = models.EmbeddedField(model_container=Person)
If I run the below query (Without providing the salary field at all):
Department(
name='Networking',
persons = {
'name': 'Mike',
'surname': 'Jordan'
}
).save()
I get a Validation error for not supplied field (Since is marked as default=0
, why django asks for it ?):
django.core.exceptions.ValidationError: ['{\'salary\': [\'Value for field "Project.Person.salary" not supplied\']
Upvotes: 0
Views: 974
Reputation: 161
I also had this issue when using MongoDB and Djongo and when trying to perform my migrate step.
It seems that Django kept some outdate definitions of my models in the my database (in the collections "django_..."), in which the "now nullable" field was "not nullable".
The workaround solution I found was to delete manually all "django_..." collections from my database and doing makemigrations + migrate back again. Then it worked.
Upvotes: 1