News for hackers. Stuff that matters.
Re:Django Styleguide by olau on Wednesday January 11, 2023 @09:14AM (#34338681)
I think there are too many concepts in this, but rather than being negative, here are some tips:

Always keep your models slim. Don't stuff template related stuff in there. You need to look at those models often, so compact is a win. course_has_finished(course) is not much longer than course.has_finished(), and will allow you to expand the functionality as time goes on. Do precomputation if you need the information in a template - that keeps your templates simpler and allows you to easily expand the complexity of the precomputation.

Don't use class-based views, at least not outside very specific niches like the Django admin. Class-based views will transform a simple, composeable call stack into a ball of inheritance mud. Inheritance is not a good code reuse tool.

Don't make separate apps in the same project, unless the project actually consists of several different, completely independent projects. You can make subdirectories without making apps, and thus avoid dependency hell.

Also be wary of the formerly South, now built-in migrations stuff. It's built around a fragile model (perfect history representation), so has lots of foot guns.

And be wary of 3rd party libraries with their own models. You can spend a lot of time trying to built bridges to those models compared to just doing what makes sense for your particular project. I think 3rd party libraries are perhaps best implemented without concrete models - duck-typing in Python let us do this. This includes Django itself, by the way. User profiles didn't become good until Django allowed you to define the user model yourself.

Re:Django Styleguide by VWWHFSfQ on Wednesday January 11, 2023 @09:40AM (#34338940)
Agree with migrations. I've spent a lot of time doing migration surgery to get things back into a consistent state. But mostly I've found that if you just let Django manage the database how it wants to then things will be fine. But get very familiar with merge migrations and fake migrations if things start to go awry.

I disagree with the perspective on CBVs though. I've been programming Django since 0.96 and CBVs have made nearly everything easier and better for me.

Re:Django Styleguide by drcongo on Wednesday January 11, 2023 @09:22AM (#34338756)
Agree with all of this, although I do like class based views.
Re:Django Styleguide by nicolaslem on Wednesday January 11, 2023 @11:55AM (#34340753)
Me too, I think it's important to acknowledge that no approach is perfect. Pick your poison.
Re:Django Styleguide by rlawson on Wednesday January 11, 2023 @09:55AM (#34339089)
Good points. Agree with slim models and I personally like a service layer. FBVs are great, CBVs are ok, GCBVs are of the devil
Re:Django Styleguide by roland35 on Wednesday January 11, 2023 @10:12AM (#34339274)
Is there a better alternative to South (built-in now) for migrations? I always had issues but figured it was the de facto for a reason.
Re:Django Styleguide by Daishiman on Wednesday January 11, 2023 @10:28AM (#34339479)
> Don't use class-based views, at least not outside very specific niches like the Django admin. Class-based views will transform a simple, composeable call stack into a ball of inheritance mud. Inheritance is not a good code reuse tool.

People always say this but well-structured CBVs keep a generic interface that you'll be really glad that exists when you have 80 views spread across 10 apps.

Composing function-based views is a PITA and when you're building an API with a bunch of auth/serialization/cache extras being bolted on it's way easier to keep disciplined and ordered. It is _trivial_ to mess up the order of callers for these things inside function-based views.

Re:Django Styleguide by rajasimon on Wednesday January 11, 2023 @10:41AM (#34339658)
> Don't make separate apps in the same project, unless the project actually consists of several different, completely independent projects. You can make subdirectories without making apps, and thus avoid dependency hell.

I wrote an article on this. My goto strategy is to create a project/core/views.py,models.py,apps.py,tests.py

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @11:32AM (#34340324)
> I wrote an article on this.

Link or it didn't happen :)

Re:Django Styleguide by rajasimon on Thursday January 12, 2023 @10:07AM (#34354550)
lol forgot to include
Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @11:49AM (#34340644)
Re:Django Styleguide by rajasimon on Thursday January 12, 2023 @10:07AM (#34354545)
yup that's that. Thanks for finding it for me :)
Re:Django Styleguide by randlet on Wednesday January 11, 2023 @10:44AM (#34339701)
"Also be wary of the formerly South, now built-in migrations stuff. It's built around a fragile model (perfect history representation), so has lots of foot guns."

My experience has been opposite so I'd be interested in hearing your experiences if you are willing. I have been using the built in migrations since day 1 on a medium sized Django project with 350+ migrations and migration issues for our project have been exceedingly rare. edit: We have a small team of developers, so merge migrations are very rare for us, which might be a contributing factor.

Re:Django Styleguide by rowanseymour on Wednesday January 11, 2023 @11:00AM (#34339884)
Same here. If I was starting a non-Python project tomorrow I'd consider using Django to manage the database schema - especially now that we can describe custom indexes and constraints in migrations. Our project has gone thru over 1000 migrations so far tho we squash them down to 50 or so about once a year.
Re:Django Styleguide by jbellew on Wednesday January 11, 2023 @12:01PM (#34340864)
Thats interesting, if I was using TypeScript to access the data, how would I keep the schemas in sync between TS and python?
Re:Django Styleguide by anileated on Wednesday January 11, 2023 @12:40PM (#34341491)
Can introspect and export Django models to JSON schema or similar, then in TS read it and use compiler low-level API to generate types. There may be libraries for either stage…
Re:Django Styleguide by rowanseymour on Wednesday January 11, 2023 @12:12PM (#34341008)
I've never used TypeScript to talk to a database but there might be tooling to generate classes from tables. Even if there isn't, manually keeping some TypeScript classes in sync might still be worth the effort, for being able to manage schema migrations easily elsewhere.
Re:Django Styleguide by dnadler on Wednesday January 11, 2023 @12:12PM (#34341019)
We do this manually along with a pydantic as a middleman between Django and TS. Works pretty well and is not a major inconvenience to keep things aligned.
Re:Django Styleguide by replygirl on Wednesday January 11, 2023 @01:11PM (#34342061)
if using DRF you export openapi to json and use openapi-typescript-codegen

if using graphene or strawberry you export sdl to json and use @graphql-codegen/typescript

Re:Django Styleguide by crucialfelix on Wednesday January 11, 2023 @03:02PM (#34343810)
Prisma works very well and has usable migrations. Not as solid as Django's.

Tiangolo of FastAPI fame is working on https://sqlmodel.tiangolo.com/ Which is pydantic models, SQL alchemy. Migrations are coming soon. This will probably be an excellent way to build APIs with db. Then you can generate a typescript client from the built in OpenAPI schema.

Re:Django Styleguide by theptip on Wednesday January 11, 2023 @12:43PM (#34341550)
Diamond-dependencies are hard to work with, and so we forbade them. Specifically, you can't revert an individual migration, you just specify your desired "target" to roll back to, and therefore you can't unapply a single branch of a diamond dependency. This means if your second branch of a diamond dependency breaks the DB, but your app depends on the first branch, you're SOL and are now manually running SQL to fix your production DB. (Can you tell I'm speaking from experience here? :) )

Your migration code uses the model classes, but the migrations are using a rehydrated version of the model that doesn't include any methods; another footgun. Basically you need to copy in any logic that you're using into your migration file, or else that migration's logic will change as your code is refactored. You might naively think that because `model.do_the_thing()` works now, the migration is somehow pickling a snapshot of your model class. It's not.

Because of the above, you should really squash migrations frequently, but it's a big pain to do so -- particularly if you have dependency cycles between apps. ("Just Say No to Apps" is my default advice for new Django developers. If you have a real need for making a bit of library code sharable between multiple different Django projects then you know enough to break this rule. Within a single monolithic project, apps just cause pain.) Squashing migrations quickly gets to some extremely gnarly and hard-to-work-with logic in the docs.

Moving models between apps isn't supported by the migration machinery; it's an involved and dangerous process. One idea here that might save Apps is if you manually remove the App Prefix from your "owned" / internal apps; if I have Customer and Ledger apps, I don't really need to namespace the tables; `user`, `information`, `ledger_entries` are fine table names instead of `customer_user`, `customer_information`, `ledger_ledger_entries`, a normal DB admin would not namespace the table names. You neeed the app namespacing to make it safe to use someone else's apps, but I think namespacing for your own apps inside a single repo is harmful.

I find the migration framework to be worth using, but it's definitely got some sharp edges.

Re:Django Styleguide by randlet on Wednesday January 11, 2023 @01:42PM (#34342564)
Great answer thank you!
Re:Django Styleguide by rowanseymour on Wednesday January 11, 2023 @01:58PM (#34342820)
Maybe you need to turn this into an article because over the last decade of working with Django, we've learnt the same lessons, sometimes the hard way. Learning to never import real models into data migrations was a big one.

I recently wanted to move a model between apps and ended up going the route of create new table, copy all rows over, delete old table. It was annoying, but the only way to make it work with regular migrations.

We ended up writing our own script[1] to squash migrations, and I'd love to know if there's a better way. We needed something that works for clean installs or existing installs that already have the current migrations installed - so it generates empty migrations which get applied on existing installs, and then they get replaced with real initial migrations on clean installs starting from a new release.

1. https://github.com/nyaruka/rapidpro/blob/main/tools/squash_m...

Re:Django Styleguide by switch007 on Wednesday January 11, 2023 @11:02AM (#34339913)
Apps should be buried deep in the documentation in some “super advanced” section and not advocated much at all
Re:Django Styleguide by rowanseymour on Wednesday January 11, 2023 @11:13AM (#34340044)
> You can make subdirectories without making apps, and thus avoid dependency hell.

It's hard to get 100% right and thus our projects always have a few lazy foreign key relationships and inline imports to avoid cyclical imports... but I think our code is easier to manage because we try to model the dependency relationships by having things in separate apps.

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

Our experience from South to 4.x has been that the models/migrations system has matured significantly and is probably now the main selling point for Django for us.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @11:34AM (#34340363)
I'm currently in circular import hell. My business logic has Jobs and Loads, and they both need to update each other under certain circumstances. Should these two things/monstrosities be lumped into the same app?
Re:Django Styleguide by hbrn on Wednesday January 11, 2023 @11:59AM (#34340842)
The short answer is yes.

The long answer is if two entities are updating each other you might benefit from shifting all update responsibilities to one of them. Or even to a third entity that knows about both and keeps those two isolated from each other.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @12:04PM (#34340910)
Woof. Thank you so much. I like the idea of a third party, like a mediator.

Would that mediator be another app? Or should it be some module sitting in the project directory? (I'm not even sure Django would import something like that.)

Re:Django Styleguide by hbrn on Wednesday January 11, 2023 @12:26PM (#34341209)
> Would that mediator be another app?

Yes!

It is an app that might not even have any model classes. But it will contain business logic. And it will probably speak domain language, which is great.

If you're lucky, those two other apps will become pluggable. You will probably never replace them, but separation of concerns is always nice.

The downside of course is that you will have 3 apps instead of 1. That's the balance you have to maintain.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @12:42PM (#34341521)
So, the way I understand it, job/services.py and load/services.py depend on mediator/mediator.py which depends on job/models.py and load/models.py, instead of job/services.py ultimately using load/services.py, and vice versa.

Thanks so much!

Re:Django Styleguide by hbrn on Wednesday January 11, 2023 @01:06PM (#34341987)
This is good enough to break circular dependencies between individual modules, but keep in mind that circular dependencies between the apps remain (e.g. job depends on mediator, mediator depends on job).

I usually prefer to resolve those too. If job is small enough, all the orchestration of jobs should happen through mediator (same for load). If it's not plausible, then job can emit signals which mediator subscribes to.

A good place to start is to give a more descriptive name to the mediator. Sure, it mediates between the two, but it probably does that to implement some business process. Can you name it after that process?

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @01:32PM (#34342399)
> ...all the orchestration of jobs should happen through mediator (same for load)

So, in a smaller app, when a request comes into job/views.py or load/views.py then we immediately start working with JobLoadMediator to handle business logic between the two? I was just going to focus on specific tasks between job and load. I'll probably look into signals; I haven't used those in several years and as I recall, it felt hacky.

> give a more descriptive name to the mediator

When a Job is deleted, it needs to delete associated Loads. And the states of the Loads will affect the state of the Job. That's the main cycle I'm looking at right now.

Re:Django Styleguide by hbrn on Wednesday January 11, 2023 @03:22PM (#34344085)
> when a request comes into job/views.py or load/views.py then we immediately start working with JobLoadMediator to handle business logic between the two?

In my world, mediator.views and job.views would likely have different audiences.

mediator.views is for business domain (e.g. your API). Though name would not be mediator, it would be something domain-specific.

job.views could be for more low-level internal tooling (e.g. analytics/monitoring). Or it could be empty, if job is just dumb data object that doesn't require public API.

> When a Job is deleted, it needs to delete associated Loads. And the states of the Loads will affect the state of the Job

If you want to keep them apart, signals is the right (though not the easiest) answer. job owns (depends on) loads, and subscribes to loads signals. load doesn't know anything about job (ignore the fact that job is referenced in load.models as foreign key, that's just limitations of SQL DDL).

The easiest is a subtle dependency: load can access job through foreign key, job can access loads through related_name. Circular dependency is still there, but it is resolved at runtime. This will start to cause pain when application grows, but is fine at small scale.

Though again, I'd probably merge those apps: if you can't reason about load without job, and can't reason about job without load, there's very little benefit in keeping them separate. It might make you feel more organized, but the if the code is lying about your mental model, this is false organization.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @04:17PM (#34344997)
> mediator.views is for business domain (e.g. your API) > job.views could be for more low-level internal tooling

That's very interesting. I'm mostly following the architecture from: https://phalt.github.io/django-api-domains/styleguide/

I knew this was going to be a large project, about 19 apps, supporting a trucking and inventory web/mobile app, and I wanted a sane/organized way to deal with all of the models and relations.

Do you know of some blog posts or books that go into the way you normally organize things?

> if you can't reason about load without job, and can't reason about job without load

There is a lot of interaction between all of the parts of the app, but particularly between Jobs, Loads, Inventory, Stages, and Drivers. In the future I might start with one app, and then add another if I absolutely have to.

Re:Django Styleguide by funkydata on Thursday January 12, 2023 @04:09AM (#34350980)
If you try to keep everything in one app, you'll have gigantic views.py, models.py, etc.

Nobody wants to work with those. Also, a simple typo in those files can bring your whole system down and can make it hard to debug.

Apps, are a cheap (EXCEPT when you HAVE TO [but really, do you? Really?] move models between apps) way to keep YOU sane.

Re:Django Styleguide by cjohnson318 on Friday January 13, 2023 @12:51PM (#34371263)
Right, no one wants to work on a 10k line view.py file. However, if you put everything into its own app, like I did, then you run into circular dependencies as your project gets closer to feature complete. So, the answer is somewhere in the middle. At the moment, I have about 20 apps, with 1 to 4 models per app, and 20% of all that is highly interdependent. I should have put the big, highly interdependent pieces in one app, and have the less connected pieces in separate, bare-bones crud-apps.
Re:Django Styleguide by funkydata on Thursday January 12, 2023 @04:05AM (#34350945)
Although it goes against certain opinions AND if you set a norm for their use, signals are the way to go.

You can either decide that signals code will live in the app where the objects reside or the app of the target objects and that's it. A built-in and simple interface between objects.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @01:56PM (#34342805)
Holy bovine. This finally worked. I've been working around the clock on this for two days now. Thanks again stranger!
Re:Django Styleguide by funkydata on Thursday January 12, 2023 @04:11AM (#34350990)
Also, do not forget to integrate with Sentry [https://sentry.io/] or something that does the same thing.

It will also keep you sane.

Re:Django Styleguide by rowanseymour on Wednesday January 11, 2023 @12:07PM (#34340948)
Well you're writing Python so you're never truly stuck when you run into dependency issues, but if you feel like you're in hell then maybe they do belong in the same app. All complex real world apps have complex dependencies between entities - all I would argue is that "put them all in the same package" generally isn't the most scalable solution.
Re:Django Styleguide by rglullis on Wednesday January 11, 2023 @11:54AM (#34340731)
> Always keep your models slim.

As simple as possible, but no simpler. Django models are meant to deal not just with the data, but also with business logic. If `course.has_finished` is a property of the course, why would you want to have a separate function outside of the class?

> Do precomputation if you need the information in a template

If the precomputation is only needed in a template, you can (should, IMO) use template tags.

> Don't make separate apps in the same project (...) avoid dependency hell.

My current pattern here has been to create one "core" project where I represent all the internal models of the domain of the application, and "adapter" apps if I want to interface/integrate with anything from the external world. This makes it easier to extend or replace third-party tools.

> (Migrations) It's built around a fragile model.

I wouldn't call it fragile, quite the opposite. There are some annoying limitations for sure (I didn't find a reliable way to change the primary key of a model, except for creating a whole new model and migrating the data to it), but I think they are due to a matter of strong safety that the migration can only be done if it consistent.

Re:Django Styleguide by well1912 on Wednesday January 11, 2023 @12:03PM (#34340890)
At my current company, we've had many teams over the years fail to make business logic in model methods work, and I think many other people have had similar results. The issues usually boil down to some combination of "business logic is too coupled to the data model" and "this method lives at an intersection of these two models and creates weird dependency problems". I now feel that Django puts you down a path for failure by naming the DB layer "models" and not giving users a decent place to put cross-model domain logic.

My current preference is a functional core-imperative shell-style architecture where as much code lives in the functional core as possible. It's not very elegant with Django but it works fine. Cosmic Python (really accessible and fairly quick read if you have the time: https://www.cosmicpython.com/book/preface.html) has examples that are similar.

Re:Django Styleguide by rglullis on Wednesday January 11, 2023 @12:27PM (#34341238)
> The issues usually boil down to some combination of "business logic is too coupled to the data model" and "this method lives at an intersection of these two models and creates weird dependency problems".

Refactor is not a dirty word. The problems you are describing seem to be more of the nature of having too many things concentrated at specific model classes, and that this model should be decomposed, broken down. This is not a Django-specific issue.

Re:Django Styleguide by lowercased on Wednesday January 11, 2023 @01:01PM (#34341909)
> I now feel that Django puts you down a path for failure by naming the DB layer "models" and not giving users a decent place to put cross-model domain logic.

I think a lot of 'MVC-inspired' frameworks fail there, not just django. Rails... 'app/helpers' maybe? Laravel 'models' is it, and 'services' or a variation is something I see a lot of folks adding, but it's not an out of the box convention. I can't remember anything specific/explicit in the asp.net world either.

Re:Django Styleguide by matsemann on Wednesday January 11, 2023 @01:47PM (#34342648)
> If `course.has_finished` is a property of the course, why would you want to have a separate function outside of the class?

Because one should avoid passing Django models around. It leads to bad design. Have a selector or something that uses the ORM, but exposes some dataclass or pydantic model instead, and put the logic there.

Re:Django Styleguide by traverseda on Wednesday January 11, 2023 @02:14PM (#34343063)
You want to add an ORM to the ORM? Why?
Re:Django Styleguide by matsemann on Wednesday January 11, 2023 @03:38PM (#34344355)
How is that adding an ORM to the ORM? I want all django orm access to happen at defined places, instead of the spaghetti mess it is when people do SomeOtherModulesModel.objects.filter(..) and expose themselves to the internal workings of that module. Access it through a selector instead.
Re:Django Styleguide by rglullis on Wednesday January 11, 2023 @03:44PM (#34344450)
If for some strange reason your application has data that it was not created with Django, sure.

But aside from that you are just adding another layer of abstraction that does not give any benefit when all your models are managed by Django already.

Re:Django Styleguide by matsemann on Thursday January 12, 2023 @02:44AM (#34350390)
It gives a huge benefit, and not doing it is why most django code is incomprehensible and slow.

No longer should anyone in their module directly do something to a model and save it. They should always go through a service in the module owning that model, that makes sure everything is done correctly. So services.py and selectors.py works as a public API for the module, while the models are internal. Avoids having lots of other apps/modules depending on your app's internals.

Re:Django Styleguide by rglullis on Thursday January 12, 2023 @07:47AM (#34352691)
> not doing it is why most django code is incomprehensible and slow.

> No longer should anyone in their module

> they should always go through a service

Weasel words and opinions-as-fact. Come back when you have a way to show that your approach gives any actual benefit.

Re:Django Styleguide by matsemann on Thursday January 12, 2023 @09:42AM (#34354148)
I did give an example. What you call opinions-as-fact was me trying to explain the approach, not commanding anything. That you disagree (or can't comprehend it?) doesn't make it weasel words. Please don't behave like this towards me, read the guidelines. You can find a link at the bottom of this page. I'll leave this "discussion" here as it's unfruitful when you act so hostile.
Re:Django Styleguide by rglullis on Thursday January 12, 2023 @10:06AM (#34354534)
If your idea of "explaining the approach" does not have address the "why?" and depends on "should always X" and "should never Y", then you are resorting to weasel words.

"incomprehensible and slow". To whom? How slow? What about your approach makes it faster?

A sibling comment pointed one important aspect: Django Querysets are lazily evaluated, I find it really hard to believe that having a layer that constant marshalls and unmarshalls the data through pydantic can make anything faster than not having to fetch any data until you really need it.

Re:Django Styleguide by traverseda on Wednesday January 11, 2023 @05:59PM (#34346315)
An orm takes a selector (typically an sql query) and maps it onto an object.

What you're describing takes a selector, and maps it onto an object. Is it just that you want type hints or something?

Re:Django Styleguide by matsemann on Thursday January 12, 2023 @02:41AM (#34350379)
No, what I'm describing is functions in a selector.py, like: def get_orders_for_date(date) -> Order:

where Order is a pydantic model, not a fat django model. Other modules shouldn't know about my internal database. All other modules should use functions from this selector.py, they aren't allowed to use the OrderModel themselves directly, only the pydantic class. Because otherwise you end up with spaghetti.

Re:Django Styleguide by traverseda on Thursday January 12, 2023 @08:57AM (#34353504)
Right... so you're talking about mapping an object from your database to a pydantic object.

So you want an ORM, but you want it without a save method? Or presumably with a save method that can only be called under specific circumstances?

Re:Django Styleguide by Nextgrid on Wednesday January 11, 2023 @06:05PM (#34346388)
Keep in mind that passing around querysets has performance advantages you wouldn't get by passing around dataclasses or similar.

For example, if you do a query like Model.objects.filter(related_model__in=RelatedModel.objects.filter(...)) the ORM will only run a single query, silently converting the second one into a JOIN.

If you pass lists of "RelatedModel" however you would've had to first one one query to get that list (raising potential edge-cases with regards to atomicity and transactional isolation) and then pass the list of IDs to the outer query in an "WHERE related_model_id IN (...)", resulting in 2 queries in the end.

Re:Django Styleguide by matsemann on Thursday January 12, 2023 @10:51AM (#34355321)
That gain is often lost by people doing unoptimized queries all over the place, though, instead of a single place where the queries are optimized. And passing the fat django objects around often lead to accidental n+1 queries, since you can't really trust that looking up a property on your object doesn't do a new query. Often nicer to avoid it all, by having a gated access to the DB.

While I propose most often sending in a list of related IDs (premature optimization and all that), the function could just accept any iterable, and you from the outside could send in the lazy relatedmodel query.

Re:Django Styleguide by yiiii on Wednesday January 11, 2023 @12:47PM (#34341629)
> Always keep your models slim.

At least never put your business logic in your views, forms or even templates.

Re:Django Styleguide by collyw on Wednesday January 11, 2023 @05:13PM (#34345773)
I am in more or less in agreement, except for forms. Surely some validation could be considered business logic.
Re:Django Styleguide by dec0dedab0de on Wednesday January 11, 2023 @01:52PM (#34342729)
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.

Re:Django Styleguide by cjohnson318 on Wednesday January 11, 2023 @03:08PM (#34343900)
> 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.

Re:Django Styleguide by asalahli on Wednesday January 11, 2023 @04:11PM (#34344918)
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.
Re:Django Styleguide by collyw on Wednesday January 11, 2023 @05:07PM (#34345693)
Are you aware of https://ccbv.co.uk/? After I discovered that, class based views became easy.
Re:Django Styleguide by adamckay on Wednesday January 11, 2023 @06:35PM (#34346665)
And if you're using DRF there's https://www.cdrf.co/
Re:Django Styleguide by asalahli on Wednesday January 11, 2023 @08:04PM (#34347645)
Wow, 8 years of using Django and I did not know about either of these. Thanks!
Re:Django Styleguide by collyw on Wednesday January 11, 2023 @04:56PM (#34345528)
I disagree with almost everything you have said.

Fat models think controllers is the suggested strategy. It works well in my opinion / experience, though I guess it could get out of hand on very large projects.

There is something that feels very unnatural and unintuitive about your course_has_finished(course) versus course.has_finished() example. This was one of the principles behind OOP, keeping your data and functions / methods together, though I know OOP isn't trendy these days. It's far more natural to have it as a method of course than some random function, stored who knows where. I worked on a system with this type of design, it was pretty bad.

One thing I would like to see for larger projects is the ability to easily split models into separate files - a bit more like Java does with one class per file. Maybe you can do this already.

Class based views mean that a lot of code is written and tested for you. I'll agree that your view does need to be somewhat "standard" in what it's doing (anything you would see in the admin, list, create, edit, detail, login, etc), so if you have something more complex multiple forms in one view, then thy don't give a great deal of advantage. In that situation I would still likely choose a basic View class over functional views, but more for consistency.

I am probably in agreement on separate apps.

Migrations are one of the best features of Django. I have just spent 4 years working on a a system without them and it was a shambles as you would expect. Everyone is scared to make database changes, so you get a ton of shitty application code to compensate for shitty database modelling. Tech debt in other words.

I can't think of any 3rd party apps where I have used the models directly, or if I did it was frictionless enough for me not to remember. So no real opinion on that one. 3rd party apps can be pretty hit and miss, especially if they get abandoned as you upgrade Django, so I would say use with caution, particularly for more obscure ones (just been revisiting django-celery-beat, that's been around for years so I doubt it's going away).

Re:Django Styleguide by __pache__ on Wednesday January 11, 2023 @07:08PM (#34347042)
how is this possibly the highest upvoted thing here on django. it's like the opposite of good opinions.. they're bad opinions!

_(99% kidding, but i disagree with most of this lol)_