MongoEngine / MongoEngine/mongoengine

should it assign default value for fields in __only_fields but without data in mongodb?

Open
#1,063 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

In [19]: class Foo(models.Document):
   ....:     foo = models.ListField()
   ....:     

In [20]: Foo.objects.delete()
Out[20]: 0

In [21]: foo = Foo.objects.create()

In [22]: foo.foo
Out[22]: []

In [23]: foo.update(unset__foo=1)
Out[23]: 1

In [24]: foo = Foo.objects.only('foo').first()

In [25]: foo.foo

In [26]: 

there is an issue fixed by https://github.com/MongoEngine/mongoengine/issues/399

but you can see line#23 will remove foo from mongodb
then i want to get Foo with foo field only, now foo will be in __only_fields and skip assign default value in https://github.com/MongoEngine/mongoengine/blob/v0.10.0/mongoengine/base/document.py#L93
but values have no foo value either https://github.com/MongoEngine/mongoengine/blob/v0.10.0/mongoengine/base/document.py#L111
so it have not assign any value for foo

i don't know the rule how to handle this situation, but it is a little annoying that i have to check foo.foo before i can use it as usual

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the __only_fields case from the issue, then inspect mongoengine/base/document.py around lines 93 and 111 to trace default assignment when the selected field is absent from MongoDB. Check existing tests for partial document loading and define the expected behavior for Foo.objects.only('foo').first(); done means the missing ListField is handled consistently without requiring a pre-access check.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.