• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Concepts in Django
 

Concepts in Django

on

  • 303 views

Decouple Django apps with business logic to have better reusability.

Decouple Django apps with business logic to have better reusability.

Statistics

Views

Total Views
303
Views on SlideShare
299
Embed Views
4

Actions

Likes
1
Downloads
9
Comments
0

1 Embed 4

http://localhost 4

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Concepts in Django Concepts in Django Presentation Transcript

    • Concepts in Django Douwe van der Meij Goldmund, Wyldebeast & Wunderliebe vandermeij@gw20e.com github.com/douwevandermeij
    • Project INSTALLED_APPS = ( ... 'crm', 'webshop', ... )
    • Dependencies ● Inter-app foreign key constraint crm Customer Project account_nr ● How to reuse webshop without crm? Order order_nr webshop OrderLine
    • Concepts Define: ● Producers ○ Contain data Producer ● Consumers ○ Don’t contain data ○ Need a producer Consumer
    • Customer concept The concept we’re talking about: ● A customer ○ Has a number
    • Applications with concepts crm Producer Customer Project account_nr webshop Consumer Customer number Order OrderLine
    • Define Producers Conceptname CONCEPTS = { Producer model 'CustomerConcept': { 'crm.models.Customer': { 'number': 'account_nr', Field mapping }, }, } ● Map to existing models
    • Consume class Order(models.Model): customer = models.ForeignKey(CustomerConcept) order_nr = models.IntegerField()
    • Dependencies crm ● ORM level Customer Customer CustomerConcept concepts Order webshop Project
    • Dependencies crm Customer Order webshop Project ● Database level
    • 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
    • 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}), }, ) ...
    • 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
    • The magic (4/4) import concepts ● Bind the concept to the concepts module ... setattr(concepts, concept_name, concept)
    • Conclusion ● Concepts make apps reusable ○ Without dependencies ● The only dependency ○ Is a concept ● The concept producer ○ You may choose yourself ○ On existing apps
    • 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
    • Thank you! Douwe van der Meij Goldmund, Wyldebeast & Wunderliebe vandermeij@gw20e.com github.com/douwevandermeij