Why your Django models are fat

Because:

  • You moved everything out of the views.py modules when you heard fat controllers were bad.

  • And you’ve read that fat models are a good idea1.

  • You need some extra functionality in a template and this is the easiest way to shoehorn it in. Eg:

<p>The current balance is {{ account.get_balance_via_three_network_calls_lol }}</p>
  • You can quickly solve your current problem by adding n more lines to that method (repeat ad infinitum)2.

  • You’re scared people will ignore your crucial wrapper function if it’s not directly on the model3.

  • Are you even allowed to put application logic anywhere but a models.py?4


  1. For example:

    However, in practice, they’re a bad idea (although this sometimes takes a few months to become apparent).

    [return]
  2. Requires a poor-quality test suite and diligent ignorance of the Single Responsibility Principle.

    [return]
  3. A legitimate problem with a liberal language like Python where it’s hard to enforce calling conventions as nothing is truly private.

    [return]
  4. You are: your web framework is not your boss.

    As a rule-of-thumb, your application logic should live in modules that aren’t Django-specific modules (eg not in views.py, models.py or forms.py). If I had my way, Django would create an empty business_logic.py in each new app to encourage this.

    [return]
----

Something wrong? Suggest an improvement or add a comment (see article history)
Tagged with: django
Filed in: lists

Previous: Bash error reporting
Next: Using a custom Sentry client

Copyright © 2005-2017 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.