Skip to content

Commit

Permalink
Added handling for callable collections
Browse files Browse the repository at this point in the history
  • Loading branch information
rozza committed May 24, 2011
1 parent 607711c commit c62b632
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mongoengine/base.py
Expand Up @@ -297,6 +297,11 @@ def __new__(cls, name, bases, attrs):
# DocumentMetaclass before instantiating CollectionManager object # DocumentMetaclass before instantiating CollectionManager object
new_class = super_new(cls, name, bases, attrs) new_class = super_new(cls, name, bases, attrs)


# Allow dynamically-generated collection names. Pass the newly
# created class so the callee has access to __module__, etc.
if callable(meta['collection']):
new_class._meta['collection'] = meta['collection'](new_class)

This comment has been minimized.

Copy link
@rozza

rozza Jul 18, 2012

Author

@ra21vi its not broken - thats the intent. A Class can only ever have a single collection.

If you need different please add a ticket.


# Provide a default queryset unless one has been manually provided # Provide a default queryset unless one has been manually provided
if not hasattr(new_class, 'objects'): if not hasattr(new_class, 'objects'):
new_class.objects = QuerySetManager() new_class.objects = QuerySetManager()
Expand Down
17 changes: 17 additions & 0 deletions tests/document.py
Expand Up @@ -61,6 +61,23 @@ class Person(Document):
# Ensure Document isn't treated like an actual document # Ensure Document isn't treated like an actual document
self.assertFalse(hasattr(Document, '_fields')) self.assertFalse(hasattr(Document, '_fields'))


def test_dynamic_collection_naming(self):

def create_collection_name(cls):
return "PERSON"

class DynamicPerson(Document):
name = StringField()
age = IntField()

meta = {'collection': create_collection_name}

collection = DynamicPerson._get_collection_name()
self.assertEquals(collection, 'PERSON')

DynamicPerson(name='Test User', age=30).save()
self.assertTrue(collection in self.db.collection_names())

def test_get_superclasses(self): def test_get_superclasses(self):
"""Ensure that the correct list of superclasses is assembled. """Ensure that the correct list of superclasses is assembled.
""" """
Expand Down

3 comments on commit c62b632

@robspychala
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the documentation it shows using the above code in a Mixin... but unfortunately it doesn't seem to work

http://mongoengine.org/docs/v0.5/upgrade.html#default-collection-naming

@rozza
Copy link
Author

@rozza rozza commented on c62b632 Mar 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out - had a name collision in the test suite that should have caught this. I've fixed in the master branch

@rozza
Copy link
Author

@rozza rozza commented on c62b632 Mar 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out - had a name collision in the test suite that should have caught this. I've fixed in the master branch

Please sign in to comment.