Your SlideShare is downloading. ×
Concepts in Django
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Concepts in Django

417

Published on

Decouple Django apps with business logic to have better reusability.

Decouple Django apps with business logic to have better reusability.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
417
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Concepts in Django Douwe van der Meij Goldmund, Wyldebeast & Wunderliebe vandermeij@gw20e.com github.com/douwevandermeij
  • 2. Project INSTALLED_APPS = ( ... 'crm', 'webshop', ... )
  • 3. Dependencies ● Inter-app foreign key constraint crm Customer Project account_nr ● How to reuse webshop without crm? Order order_nr webshop OrderLine
  • 4. Concepts Define: ● Producers ○ Contain data Producer ● Consumers ○ Don’t contain data ○ Need a producer Consumer
  • 5. Customer concept The concept we’re talking about: ● A customer ○ Has a number
  • 6. Applications with concepts crm Producer Customer Project account_nr webshop Consumer Customer number Order OrderLine
  • 7. Define Producers Conceptname CONCEPTS = { Producer model 'CustomerConcept': { 'crm.models.Customer': { 'number': 'account_nr', Field mapping }, }, } ● Map to existing models
  • 8. Consume class Order(models.Model): customer = models.ForeignKey(CustomerConcept) order_nr = models.IntegerField()
  • 9. Dependencies crm ● ORM level Customer Customer CustomerConcept concepts Order webshop Project
  • 10. Dependencies crm Customer Order webshop Project ● Database level
  • 11. The magic (1/4) from django.conf import settings for concept_name, mapping in settings.CONCEPTS.items(): for klass, fields in mapping.items(): ... ● The concepts app ○ Loops over settings.CONCEPTS
  • 12. The magic (2/4) from concepts.util import load_class ● Create a dynamic class ● With in ... concept = type( proxy = True Meta concept_name, (load_class(klass),), { '__module__': __name__, 'Meta': type('Meta', (), {'proxy': True}), }, ) ...
  • 13. The magic (3/4) ... for _property, _field in fields.items(): if not hasattr(concept, _property): setattr( concept, _property, property( lambda self: getattr(self, _field), lambda self, value: setattr(self, _field, value), ), ) ... ● Create properties for all fields
  • 14. The magic (4/4) import concepts ● Bind the concept to the concepts module ... setattr(concepts, concept_name, concept)
  • 15. Conclusion ● Concepts make apps reusable ○ Without dependencies ● The only dependency ○ Is a concept ● The concept producer ○ You may choose yourself ○ On existing apps
  • 16. Try it yourself ● github.com / douwevandermeij / concepts_example ● Related work ○ Django Swappable Models ■ Like swapping auth.User model in 1.5+ ○ Zope Component Architecture ■ Interfaces: implement/adapt
  • 17. Thank you! Douwe van der Meij Goldmund, Wyldebeast & Wunderliebe vandermeij@gw20e.com github.com/douwevandermeij

×