Created
November 14, 2011 16:56
-
-
Save alecperkins/1364430 to your computer and use it in GitHub Desktop.
Simulated ForeignKeys from MongoEngine Document to Django Models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using the `@property` decorator allows the MongoEngine Document object to have | |
# properties that reference Models, and be got/set like normal foreign keys. | |
# This same pattern can work the other way and allow Models interface with | |
# Documents. | |
class Foo(mongoengine.Document): | |
# The `id` of the property is stored in the Document. | |
user_id = mongoengine.IntField() | |
# Setters and getters to interface with the SQL DB and set Users to the | |
# user property directly. | |
@property | |
def user(self): | |
# Lazily dereference the Model, and cache the object in the `_data` | |
# property to avoid repeated fetching. | |
if 'user' not in self._data: | |
self._data['user'] = User.objects.get(pk=self.user_id) | |
return self._data['user'] | |
@user.setter | |
def user(self, value): | |
# Cache the value in the `_data` attribute and set the corresponding | |
# `id` property. | |
if value and hasattr(value, 'pk'): | |
self._data['user'] = value | |
self.user_id = value.pk | |
else: | |
self._data['user'] = None | |
self.user_id = None | |
@user.deleter | |
def user(self): | |
self._data['user'] = None | |
self.user_id = None |
Yeah I don't blame you: I'm using PyMongo directly for some things (particularly where speed is a concern). Anyway I think it's even simpler than that, you can basically just write a custom property decorator that sets the attribute on the given model in the scope of the fget method. So long as this is executed at some point, because it's using the customized mongoengine descriptor logic, it will actually be able to query by it. One issue with this approach is that it doesn't work until it's been initialized via getattr for example. Anyway thanks for indulging me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can. Just make your own derivative of
BaseField
and define the necessaryto_mongo
andto_python
methods that do the translation, as specific to your data model. This simulated foreign key thing could be done as a custom field, but it was simpler to use the@property
decorator. (I've actually since moved away from using MongoEngine entirely — just using PyMongo directly, with DictShield for validation help.)