2
votes

Here is the senario. I have a model called Permission. Lets assume it looks like this:

class Permission(Document):
    meta = {'collection': 'permission'}
    permission = DictField()
    owner = ReferenceField('user')

This inherits from the mongoengine's Document class. Thus when I save an instance of this, a collection is automatically created named permission in the database.

I want to be able to change the collection name dynamically. For example if I had a Contacts model and wanted to save the permission for the Contact model I dont want it to go into the permission collection but be saved in a separate collection called contact_permission.

I have used the feature/dynamic_collection_naming branch for mongoengine. I am not able to figure out how to get this working. I have tried this:

p = Permission()
...
p.meta['collection'] = 'contact_permission'
p.save()

But this does not work. Could you help me out please.

1

1 Answers

1
votes

That's not the intended API for the dynamic collection naming. Quick search through the commits in the branch you've mentioned shows this commit which shows how to use it:

https://github.com/hmarr/mongoengine/commit/c62b6321adaba6c3eb54ce2dc2c1bd3069568399