Share
Embed
Transcript
51 slides
-
“ “Someone told me you are writing Someone told me
you are writing everything on your views...” everything on your views...” -
- Loose Coupling - Less Code - Quick development -
Don’t repeat yourself (DRY) Django Design Philosophies[1] -
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] . -
Models should encapsulate every aspect of an “object,” following Martin
Fowler’s Active Record[5] design pattern[6] . -
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) … -
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() -
A Manager is the interface through which database query operations
are provided to Django models[8] . -
A QuerySet is iterable, and it executes its database query
the first time you iterate over it[9] . -
class SavingAccounts(View): def get(self, request): saving_accounts = Account.objects.filter( account_type='SA') return
render( request, 'core/withdraw.html', {'saving_account': saving_accounts}) -
class SavingAccounts(View): def get(self, request): saving_accounts = Account.objects.all_saving_accounts() return render(
request, 'core/withdraw.html', {'saving_account': saving_accounts}) -
"Using Django's low-level ORM query methods directly in a view
is (usually) an anti-pattern[10]* ." * - Outdated but the arguments still valid -
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] . -
We see a template system as a tool that controls
presentation and presentation-related logic[12] . -
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) -
<xml> <transactions> {% for transaction in transactions %} <transaction> {{
transaction.value }} </transaction> {% endfor %} </transactions> </xml> -
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}) -
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) -
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}) -
Django has the concept of “views” to encapsulate the logic
responsible for processing a user’s request and for returning the response[14] . -
[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) -
[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