Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore DictField and ListField when not required #2342

Open
thomasstats opened this issue Jun 3, 2020 · 4 comments
Open

Ignore DictField and ListField when not required #2342

thomasstats opened this issue Jun 3, 2020 · 4 comments

Comments

@thomasstats
Copy link

Currently if I have an object such as

class Foo(DynamicDocument): _id = ObjectIdField(required=True, default=ObjectId, primary_key=True) bar = DictField()

When I create a new item and add it I get:

{ _id: ObjectId(<objectid>), bar: {} }

I would rather this be

{ _id: ObjectId(<objectid>) }

Is there a way to do this currently? List has similar behavior by creating a [].

@sznowak13
Copy link

I think, when defining Document class you are implying a structure, similar to the relational model, so that's a logical behaviour of mongoengine.

@thomasstats
Copy link
Author

So I guess the only solution is to basically create an Embedded Document and then leave it out of the DynamicDocument definition and then provide it as a kwarg when constructing a case that involves it.

@bagerard
Copy link
Collaborator

bagerard commented Jun 14, 2020

Right, the desired behavior is to store an empty dict (or empty list, the same behavior can be observed with ListField).

Note that there is currently a flaw in the design which is somehow in your interest, the empty dict goes away if you set the field to something and then change it back to an empty dict.
See below:
image

But to be honest this is not a desired behavior, its there for historical reason but I'd like to change it in the future so I think its better to avoid building on top of it.

Have a look at DynamicDocument, it may be a better fit for achieving what you want.

I'm curious, could you elaborate on your use case? Why would it be better to store an empty dict?

@thomasstats
Copy link
Author

thomasstats commented Jun 17, 2020

I'll take a look into maybe handling it that way.

Basically I have a bunch of documents that have different attributes and I want to be able to quickly look up what documents do not have those attributes. Those attributes are strictly structured. However, I've actually restructured the documents to be a bit smaller and allow for me to just store these attributes separately.

EDIT:

Why would it be better to store an empty dict?

Or maybe you misunderstand. Say I have a series of images and some of these images have points of interest and others maybe just have boxes of interest. I am storing the images basically as:

{'imagename': imagename, 'points': points, 'boxes': boxes}

As it is now if I have an image that has no boxes, it would appear as:

{'imagename': imagename, 'points': points, 'boxes': []}

So if I want to quickly see images that only have points and no boxes, the box is still an element in it, which adds a layer of complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants