Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django responsibilities

Django responsibilities

Denis Costa

June 23, 2016
Tweet

More Decks by Denis Costa

Other Decks in Technology

Transcript

  1. “ “Someone told me you are writing Someone told me

    you are writing everything on your views...” everything on your views...”
  2. - Loose Coupling - Less Code - Quick development -

    Don’t repeat yourself (DRY) Django Design Philosophies[1]
  3. A model is the single, definitive source of information about

    your data. It contains the essential fields and behaviors of the data you’re storing[4] .
  4. Models should encapsulate every aspect of an “object,” following Martin

    Fowler’s Active Record[5] design pattern[6] .
  5. class Account(models.Model): ACCOUNT_TYPES = ( ('CK', 'Checking Account'), ('SA', 'Saving

    Account')) balance = models.IntegerField() user = models.OneToOneField('auth.User') creation = models.DateField(auto_now_add=True) overdraft_limit = models.IntegerField() account_type = models.CharField( max_length=10, choices=ACCOUNT_TYPES) …
  6. class Account(models.Model): … def withdraw(self, amount): if self.can_withdraw(amount): self.balance -=

    amount self.save() def can_withdraw(self, amount): return self.balance + self.overdraft <= amount def change_type(self, _type): assert _type in self.ACCOUNT_TYPES self.overdraft_limit = 500 if _type == 'CK' else 0 self.account_type = _type self.save()
  7. A QuerySet is iterable, and it executes its database query

    the first time you iterate over it[9] .
  8. "Using Django's low-level ORM query methods directly in a view

    is (usually) an anti-pattern[10]* ." * - Outdated but the arguments still valid
  9. Django’s template engine provides a powerful mini-language for defining the

    user-facing layer of your application, encouraging a clean separation of application and presentation logic[11] .
  10. We see a template system as a tool that controls

    presentation and presentation-related logic[12] .
  11. class BankStatement(View): def get(self, request, account_id): account = Account.objects.get(account_id) transactions

    = Transaction.objects.filter( account=account) xml = '<xml>' xml += '<transactions>' for transaction in transactions: xml += '<transaction>' xml += transaction.value xml += '</transaction>' xml += '</transactions>' xml += '</xml>' return HttpResponse(xml)
  12. <xml> <transactions> {% for transaction in transactions %} <transaction> {{

    transaction.value }} </transaction> {% endfor %} </transactions> </xml>
  13. class BankStatement(View): def get(self, request, account_id): account = Account.objects.get(account_id) Transactions

    = Transaction.objects.filter( account=account) return render( request, 'core/statement.xml', {'transactions': transactions})
  14. class CreateAccount(View): def get(self, request): return render(request, 'core/create_account.html') def post(self,

    request): account = Account.objects.create() account.balance = 0 account.user = request.user account.overdraft_limit = 0 account.account_type = request.POST.get( 'account_type') account.save() return redirect('detail_account', id=account.pk)
  15. class CreateAccount(View): def get(self, request): form = AccountForm() return render(

    request, 'core/create_account.html', {'form': form}) def post(self, request): form = AccountForm(request.POST) if form.is_valid(): account = form.save() return redirect( 'detail_account', id=account.pk) return render( request, 'core/create_account.html', {'form': form})
  16. Django has the concept of “views” to encapsulate the logic

    responsible for processing a user’s request and for returning the response[14] .
  17. [7]

  18. [1] - https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ [2] - https://www.amazon.com/Applying-UML-Patterns-Introduction-Object-Oriented/dp/0131489062/ [3] - https://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672/ [4]

    - https://www.amazon.com/Fundamentals-Object-Oriented-Design-Meilir-Page-Jones/dp/020169946X/ [5] - https://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445/ [6] - https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ [7] - https://www.amazon.com/Two-Scoops-Django-Best-Practices/dp/0981467342/ References (books)
  19. [1] - https://docs.djangoproject.com/en/1.9/misc/design-philosophies/ [2] - http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod [3] - https://drive.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/view [4]

    - https://docs.djangoproject.com/en/1.9/topics/db/models/ [5] - http://martinfowler.com/eaaCatalog/activeRecord.html [6] - https://docs.djangoproject.com/en/1.9/misc/design-philosophies/#models [7] - https://www.dabapps.com/blog/django-models-and-encapsulation/ [8] - https://docs.djangoproject.com/en/1.9/topics/db/managers/#managers [9] - https://docs.djangoproject.com/en/1.9/ref/models/querysets/ [10] - https://www.dabapps.com/blog/higher-level-query-api-django-orm/ [11] - https://docs.djangoproject.com/en/1.9/ref/templates/ [12] - https://docs.djangoproject.com/en/1.9/misc/design-philosophies/#separate-logic-from-presentation [13] - https://docs.djangoproject.com/en/1.9/topics/forms/#django-s-role-in-forms [14] - https://docs.djangoproject.com/en/1.9/#the-view-layer References