Dynamic Models with Django


Published on

Juergen Schackmann investigates the various uses cases for Dynamic Models and describes and evaluates the various options on how to implement those with Django.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Dynamic Models with Django

  1. 1. Dynamic Modelsin DjangoJuergen Schackmannj.schackmann@reliatec.de
  2. 2. © Juergen Schackmann 2013 2/28Me▸ Python & Django since 4 years▸ reliatec.de since 3 months▸ Django Web Applications▸ Focus: EDC systems for clinical studies & Telemonitoring
  3. 3. © Juergen Schackmann 2013 3/28Objective▸ Why do do we care about Django Dynamic Models?▸ Why do we have to talk about them?▸ What should we do about them?
  4. 4. Static vs Dynamic Models
  5. 5. © Juergen Schackmann 2013 5/28Models in a Django projectPhase1:Code DevelopmentModels and modelattributes are defined byimplementing those withPython codePhase 2:DeploymentUnderlying database tablesand columns are created byrunning “syncdb” commandPhase 3:RuntimeModels / DB Tables arepopulated with content aspart of the userinteractions
  6. 6. © Juergen Schackmann 2013 6/28Problem?There is no way to adapt the Django Models at runtime based onuser interactions▸ Django Models are defined and implemented duringdevelopment phase▸ Django Models are expected to be static after deployment▸ Django DB Backends only fully support SQL databases, i.e. aschema is required▸ DB specific solutions limit adoption rate of any reusable App
  7. 7. Use Cases for DynamicModels
  8. 8. © Juergen Schackmann 2013 8/28Custom Content Types for a CMS** system: dotCms (Java); source: http://dotcms.com/
  9. 9. © Juergen Schackmann 2013 9/28Customized Products for an Ecommerce Shop** system: Magento (PHP); source: averybrandandprint.co.uk
  10. 10. © Juergen Schackmann 2013 10/28Clinical Research Forms** system: OpenClinica (Java); source: openclinica.com
  11. 11. © Juergen Schackmann 2013 11/28Problem? Which Problem?▸ Models / Content Types are domain or site specific▸ Neither realistic nor efficient to implement all thinkable contenttypes into a software product▸ Configuration by subject-matter experts is better, than codecustomization by developers▸ Dynamic Models reduce the number of deployment cycles
  12. 12. Evaluation ofPatterns & Apps
  13. 13. © Juergen Schackmann 2013 13/28As a User, I ...▸ want to maintain my dynamic models easily withoutunderstanding any Django Model details▸ want extend existing (static) models without thinking aboutstatic and dynamic parts▸ do not want to think about performance, when creating mycustom fields▸ I want to be able to query/search for certain informationseamlessly over static and dynamic models
  14. 14. © Juergen Schackmann 2013 14/28As a developer, I ...▸ want my dynamic models to be integrated with Django StandardTools (admin, migrations, reversion) out of the box▸ want to apply the same API that I use for the static models▸ do not want to use or be limited to specific databases, but usethe standard Django backends▸ want to have a customizable front-end for my users to managethe dynamic models
  15. 15. © Juergen Schackmann 2013 15/28Evaluation Criteria▸ Performance▸ “Querybility”▸ Django Standard Tools Integration (Admin, Cache, ...)▸ Supported Backend▸ Complexity/ Maintainability
  16. 16. © Juergen Schackmann 2013 16/28Entity Attribute Value (EAV)Transform your table columns into table rowsAttribute 1 Attribute 2 Attribute nTableAttributeNameAttributeValueAttributeTableTable has1 n
  17. 17. © Juergen Schackmann 2013 17/28EAV Django Apps▸ bitbucket.org/neithere/eav-django/▸ github.com/mvpdev/django-eav
  18. 18. © Juergen Schackmann 2013 18/28Serialized DictionaryTransform your table columns into pickeld or jsoned dictionariesAttribute 1 Attribute 2 Attribute nTableDictionaryStringAttributes{“attribute1”:”a”, “attribute2”: 5, “attribute3”:[1,2] }
  19. 19. © Juergen Schackmann 2013 19/28Serialized Dictionary Django AppsFields▸ bitbucket.org/schinckel/django-jsonfield/▸ github.com/bradjasper/django-jsonfield▸ github.com/derek-schaefer/django-json-field▸ github.com/shrubberysoft/django-picklefieldApp▸ github.com/dobarkod/django-dynamic-model
  20. 20. © Juergen Schackmann 2013 20/28Runtime Schema UpdatesUpdate Django models and database schema at runtime using▸ syncdb functionality▸ South functionality▸ eventually: Andrew Godwins “Schema Migrations for Django”
  21. 21. © Juergen Schackmann 2013 21/28Runtime Schema Update Django AppsUpdate Django models and database schema at runtime using▸ Syncdb▸ bitbucket.org/mhall119/dynamo/overview▸ South▸ github.com/willhardy/dynamic-models▸ github.com/willhardy/django-dymo▸ bitbucket.org/schacki/django-dynamo▸ github.com/charettes/django-mutant▸ Schema Migrations▸ github.com/charettes/django-mutant
  22. 22. © Juergen Schackmann 2013 22/28Database specific solutionsHstore▸ github.com/jordanm/django-hstore▸ postgresql.org/docs/9.0/static/hstore.htmlNo SQL▸ github.com/django-nonrel▸ github.com/pydanny/django-mongonaut
  23. 23. © Juergen Schackmann 2013 23/28SummaryEAV Serialization Schema UpdatesPerformance   Querybility   Django StandardTools / SupportedBackend  Complexity/Maintainability  
  24. 24. © Juergen Schackmann 2013 24/28Schema Update most powerful, but ...▸ Name clashes between developer models and dynamic models(especially with schema migrations)▸ Multiple Users updating the same model (possibly evenconflicting)▸ Orphan database elements▸ Dynamic Model loading at startup▸ How to find/inject dynamic models into views and templates
  25. 25. Components of aDynamic Model App
  26. 26. © Juergen Schackmann 2013 26/28Must have components of a Dynamic ModelApplication▸ Meta Model Front End to let users maintain their meta models▸ Meta Model Representation to store the meta model information▸ Meta Model Analyzer to detect changes that require some furtheractions (for schema, admin or model)▸ Schema Sync Manager to update and sync the database schema▸ Admin Sync Manager to update and sync the Django admin▸ Model/Cache Sync Manager to update and sync the Django modelsand model cache
  27. 27. © Juergen Schackmann 2013 27/28More resources on dynamic models▸ code.djangoproject.com/wiki/DynamicModels▸ djangopackages.com/grids/g/forms/▸ stackoverflow.com/questions/7933596/django-dynamic-model-fields/7934577
  28. 28. Thank you very much!Questions please?