Hacker News new | ask | show | jobs
by dec0dedab0de 1254 days ago
Always keep your models slim. Don't stuff template ...course_has_finished(course) is not much longer than course.has_finished()

I disagree with this. When trouble shooting or expanding code it is super convenient to import a model and have all of your methods on auto complete. Especially when you need the same functionality in a view, a cron job, a celery task, and an DRF end point.

If you want to keep it clean you can put all your methods in a mixin class and import it from another file.

Also be wary of the formerly South, now built-in migrations stuff.

Things are much better than they were in South. But yes be careful, rule of thumb is always move forward.

Don't use class-based views

Please. For the love of God, always use class based views for almost everything. Almost everything you need is a variant of one of the built in class based views, don't make me read your copy/pasted reimplementation of it.

2 comments

> Please. For the love of God, always use class based views for almost everything.

This. 100%. Leverage as much pre-built stuff you can, especially with something as important as your HTTP layer. Whenever I run out of CRUD verbs for a model and I need to add a custom endpoint, I'll implement it in a separate APIView sublcass. Convention over configuration; write boring code.

In my view (hehe), Django's class based views is a good idea implemented poorly. In theory you should be able to use any of the built-in class based generic views with minimal customizations to suit your needs, except when you want to do such customizations you're left dealing with a huge inheritance tree of mixins. It's all magic unless you know or wanna read the documentation on what each mixin brings to the view, that is _if_ you know what mixins are involved exactly, of course.
Are you aware of https://ccbv.co.uk/? After I discovered that, class based views became easy.
And if you're using DRF there's https://www.cdrf.co/
Wow, 8 years of using Django and I did not know about either of these. Thanks!