I'm sharing a dummy code, expecting myself to be as clear as possible.
1. I have a model like this
from django.db.models import Model, CharField, IntegerField, DateField
class Account(Model):
name = CharField(max_length=30, null=False, blank=False, unique=True)
rank = IntegerField(null=True, blank=True)
date = DateField(null=True, blank=True)
2. I have my DB settings in django like this
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'TEST',
'HOST': 'localhost'
}
}
3. After running model migration commands, in the django shell, I added two objects(documents) to the configured TEST DB like this
In [1]: from dummy.models import Account
In [2]: Account.objects.create(name="Sai")
Out[2]: <Account: Sai>
In [3]: Account.objects.create(name="Ravi", rank=1)
Out[3]: <Account: Ravi>
4. When I check in Mongodb CLI, I'm getting the null values as well
> db.dummy_account.find().pretty();
{
"_id" : ObjectId("5e4f54939069175d6ed7890e"),
"id" : 1,
"name" : "Sai",
"rank" : null,
"date" : null
}
{
"_id" : ObjectId("5e4f54a89069175d6ed7890f"),
"id" : 2,
"name" : "Ravi",
"rank" : 1,
"date" : null
}
Q. (Fields that have null should not be stored) What do I need to do to get my expected result like this?
> db.dummy_account.find().pretty();
{
"_id" : ObjectId("5e4f54939069175d6ed7890e"),
"id" : 1,
"name" : "Sai"
}
{
"_id" : ObjectId("5e4f54a89069175d6ed7890f"),
"id" : 2,
"name" : "Ravi",
"rank" : 1
}