Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Inherited queries

namlook edited this page Aug 30, 2011 · 4 revisions

Inherited queries

When we query a document, we get all documents of the same instance:

>>> users = connection.User.find() # get User instances only

The issue here is that if the fetched document is a ProfessionalUser (ie, it contains the activity field), it will be wrapped into a User instance, not a ProfessionalUser instance. This is a huge issue if we need to access to method related to ProfessionalUser.

The solution is to add the _type field into the structure:

@connection.register
class User(Document):
    __database__ = "tutorial"
    __collection__ = "users"
    structure = {
        "_type": unicode,
        "name": unicode,
        "is_registered": bool,
    }
    default_values = {"is_registered": False}

class ProfessionalUser(User):
    structure = {
        "activity": unicode,
    }

    def professional_stuff(self):
        return "stuff"

This will add the document class name into the _type field so MongoKit will know how to wrap the fetched document:

>>> user = connection.User()
>>> user
{"_type": "User", "name": None, "is_registered": False}
>>> user["name"] = u"Timy"
>>> user.save()

>>> pro = connection.ProfessionalUser()
>>> pro
{"_type": "ProfessionalUser", "name": None, "is_registered": False, "activity": None}
>>> pro["name"] = u"Bob"
>>> pro["activity"] = u"Alien"
>>> pro.save()

Now MongoKit will be able to wrap the correct document:

>>> user = connection.User.find_one({"name":"Bob"})
>>> user.professional_stuff()
"stuff"

FAQ

Hey ! I already use the _type field. Don't touch it !

No problem. Just overwrite the type_field attribute:

@connection.register
class User(Document):
    type_field = "_t"
    structure = {
        "_type": unicode,
        "_t": unicode,
        "name": unicode,
    }

If you don't planed to use inherited queries, just set type_field to None;