0

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
}
Kiran Racherla
  • 219
  • 3
  • 12
  • maybe `null=True` in your field definition in the model has something to do with it. – Red Cricket Feb 21 '20 at 04:21
  • @RedCricket, I tried taking it off and migrated again, but it doesn't seem to have any effect, this is how it created the collection: `CREATE TABLE "dummy_account" ("id" int32 NOT NULL PRIMARY KEY AUTOINCREMENT, "name" string NOT NULL UNIQUE, "rank" int32 NOT NULL, "date" date NOT NULL);` Even though it's not null, the null fields are being stored – Kiran Racherla Feb 21 '20 at 04:31
  • @KiranRacherla : I'm not quiet sure on djongo but in general on mongo model if you say `required : false` **i.e;** `blank=True` then you would not specify `default=None`, Cause you're saying a field may not be present in request by saying required false, again if you say default that means create a default value if that specific field is not present in request (if `none` resolves to `null` then that's the issue)!! Try removing `default` along with `null`(Even if you purposefully send `NULL`'s to DB - it will throw error, it you'll send NULL's keep it as is) + re-migrate & check.. – whoami - fakeFaceTrueSoul Feb 21 '20 at 04:51
  • @whoami, yep, thanks, I didn't notice this earlier but I tried taking `default=None` off and re-migrated it, but still it is storing the null valued fields. I'm gonna edit my question here – Kiran Racherla Feb 21 '20 at 04:58
  • Also tried with `'ENFORCE_SCHEMA': False`, still not getting intended result – Kiran Racherla Feb 27 '20 at 10:37

1 Answers1

0

Since the model you have created, Account has 3 fields, DjAngo will always do an insert of 'rows' (or entries) with rank and date set to None. So there is no way of knowing if the user actually set these values to None, or they were just not entered.

This is a limitation in DjAngo.

nesdis
  • 1,182
  • 13
  • 16