Django and Mongoengine

4,287 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,287
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Django and Mongoengine

  1. 1. Django and Mongoengine Austin Gross [email_address] http://loc-cit.com
  2. 2. Mongoengine <ul><ul><li>Active on GitHub </li></ul></ul><ul><ul><li>Most popular MongoDB ORM   for Python (so common gotchas are easily googled) </li></ul></ul><ul><ul><li>Django-like &quot;Document&quot; classes </li></ul></ul><ul><ul><li>Integrated with Django API Sessions, Authentication, and Storage </li></ul></ul><ul><ul><li>Can be used without Django </li></ul></ul>
  3. 3. How does it work? <ul><ul><li>Cf. screencast on setting up &quot;Mumblr&quot; at montylounge . Search for &quot;Integrating MongoDB and Django&quot; </li></ul></ul><ul><ul><li>Document schemas defined in a file of your choosing; they inherit from type &quot;Document&quot; </li></ul></ul><ul><ul><ul><li>subclasses will be stored in the same collection </li></ul></ul></ul><ul><ul><li>Call mongoengine.connect(dbname) in settings.py </li></ul></ul>
  4. 4. Schemas <ul><ul><li>Support for dynamically-schema'd Documents is in development! </li></ul></ul><ul><ul><li>Otherwise, familiar look and feel </li></ul></ul><ul><ul><li>But no JOINs: for high performance, embedding documents recommended </li></ul></ul><ul><ul><li>Lists of &quot;Embedded Documents&quot; recommended in appropriate cases </li></ul></ul><ul><ul><li>Otherwise, ListField(ReferenceField(target_doc)) </li></ul></ul>
  5. 5. Schemas II <ul><ul><li>But Embeds can't be nested manageably! </li></ul></ul><ul><ul><ul><li>With MongoDB's $ operator, it's impossible to update a single comment embedded in a reply embedded in a thread. At best, retrieve and change  all  comments and set them all at once. </li></ul></ul></ul><ul><ul><li>Embedded Documents in Mongoengine don't work like Django models, even though MongoDB encourages Embeds as an alternative to References. </li></ul></ul><ul><ul><ul><li>No .save() method--in fact, no methods at all; can only be updated by .update_one() on the parent document. </li></ul></ul></ul><ul><ul><li>Still, manageable for one layer </li></ul></ul>
  6. 6. Django Functionality <ul><ul><li>Auth, Sessions, and File serving all fine </li></ul></ul><ul><ul><li>More complicated: </li></ul></ul><ul><ul><ul><li>Caching </li></ul></ul></ul><ul><ul><ul><li>Generic views </li></ul></ul></ul><ul><ul><ul><li>Testing </li></ul></ul></ul><ul><ul><ul><li>ModelForms </li></ul></ul></ul>
  7. 7. Caching <ul><ul><li>Try django-mongodb-cache? </li></ul></ul><ul><ul><li>(I haven't) </li></ul></ul>
  8. 8. Testing <ul><li>Before unit tests, call: </li></ul><ul><li>mongoengine.connect('unit-tests') </li></ul><ul><li>...That's it! Import your models and testing works as expected on test database 'unit-tests' </li></ul>
  9. 9. ModelForms: a lot of work <ul><ul><li>Might be more complicated. A GitHub project exists: &quot;django-mongoforms&quot; </li></ul></ul><ul><ul><li>It's buggy and the last commit was in March </li></ul></ul><ul><ul><li>ReferenceField supported </li></ul></ul><ul><ul><li>No MultipleReferenceField! </li></ul></ul><ul><ul><li>No custom widgets </li></ul></ul><ul><ul><li>Workable, but takes work </li></ul></ul><ul><ul><li>Would be pretty complicated for any model we store as an Embedded Document </li></ul></ul>
  10. 10. Generic views: easily adapted <ul><li>For example, in django.views.generic.create_update: </li></ul><ul><ul><li>document._meta.app_label doesn't exist automatically, but can be added </li></ul></ul><ul><ul><li>same for document._meta.verbose_name ... or remove that from the view. </li></ul></ul><ul><ul><li>You'll have to incorporate the mongoengine-compatible version of ModelForms, since create_update  otherwise will try to use Django's ModelForm </li></ul></ul>
  11. 11. Bells and Whistles <ul><ul><li>Registration forms, etc. </li></ul></ul><ul><ul><li>Things like Ajax-based autocomplete are available for Django, not yet for mongoengine </li></ul></ul>
  12. 12. GitHub links <ul><ul><li>Mongoengine. </li></ul></ul><ul><ul><li>Try Django-mumblr for a working mongoengine application </li></ul></ul><ul><ul><li>Django-mongoforms. Some branches might be more useful than the original, so cf. the network. </li></ul></ul>

×