Hacker News new | past | comments | ask | show | jobs | submit login

The main problem with Django is where to put business logic. It's actually kinda funny to write it like that, because that's the most important thing if you want to do something that isn't just CRUD.

Quick, where would you put a high-level business function that touches multiple models? Where would you put the model-level validation for that? I've seen all of the following: in a view, in a form, in a service-layer function, arbitrarily in one of the models/managers, implicitly in the database schema (just catch the IntegrityErrror!).

There are endless discussions online about where to do this. This isn't a good sign. It should be easy. The business logic should be at the heart of an application. But instead we see Django's ORM at the heart.

Django models are really limiting and totally tied to the database. You can't even have a model that has a list/set of items without getting into ForeignKeys etc. You can't test any of it without having the database present. Why would I need the database to be there to test business logic?! The point of an ORM is to do object persistence, not to do business logic.

So I've seen people do a separate set of ORM models and domain models and then manually mapping between them. This is possible, but it's a whole lot easier with something like SQLAlchemy which is actually designed for that (it's a data mapper rather than active record type ORM).

Then there's stuff like django-admin and ModelForms etc. which are the last thing you want if you're doing more than CRUD.

Tbh it would be utterly remarkable if Django somehow made this stuff easy. But it doesn't and I find myself wishing I just put in the effort to set up the boring stuff like auth etc. but with a proper architecture.




So you want a web framework that dictates how you structure your application code? I think one of Django's strengths is that if a component doesn't work well for the application you are building, you are always free to choose something else. If the default Django app structure doesn't work well for you, you can pick any structure that does. Though I would argue that the default structure is exactly what the vast majority of web applications need.


No, I want a web framework that is just a web framework. The web is just an entrypoint to an application, like a CLI or GUI. If you use something like Flask or Pyramid this is exactly what you get. They are roughly analogous to something like Qt. Something that sits at the edge of an application converting low-level user input into high-level commands and events etc.

You could use Django like this, but you'd end up manually mapping between Django models and high-level domain models. But this mapping is exactly what an ORM like SQLAlchemy does! It seems crazy to do it yourself. So nobody does. They build fat models with all the business logic right next to application logic like foreign keys etc.

Even if you did try to do something different developers would complain that it's no longer a "Django project". Have you ever seen any Django project do business logic completely independently of application logic?


So then your answer is simple- just like in Flask, put the function wherever it makes sense for your application and desired architecture.

If you really prefer SQLAlchemy, there is nothing stopping you from using it over the Django ORM, I happen to prefer the Django ORM, but Django doesn't get jealous if you go out with a different ORM (it is a polyamorious web framework[1]).

Yes, I have seen Django applications do that. There are plenty of individuals out there advocating such a separation. I wouldn't worry about these "different developers' and their opinions.

[1] I was going to use a smiley face here, but then I recalled that a Python dev was de-flocked recently for using smiley emoji as it is offensive to some individuals, so insert whatever words or symbols you don't find offensive that indicate humor instead.


Again, I'm not saying you can't do this with Django, I'm saying it makes it much harder than it should be.

Using Django without its ORM really doesn't seem attractive. I'm really not sure what you'd be getting from Django at that point. Crap templates, views that aren't views. The ORM is the only good bit of Django. Well, that and the package ecosystem.

The more I think about it the more I realise it's that last point that is the real attraction.

Also please don't worry about offending me. I come from the old internet ;)


Now I think I get it, you just don't like Django. And that is fine, there are plenty of good alternatives. I happen to like it and find that I miss many of its small convenience functions when I don't have it. Its not perfect, but it gets the job done or gets out of the way when it doesn't- at least for me.


> Quick, where would you put a high-level business function that touches multiple models? Where would you put the model-level validation for that?

It's just python: `descriptive_name.py`. `ingest_validation.py` etc. If this seems too terse, it's because your question was posed in a general way. If you have more info on the sort of business logic you have in mind, or the sort of data validation, I'll reply with more.

Could you describe what you have in mind regarding models not tied to the database? If it's the naive interpretation I'm thinking of, use python dataclasses and enums. Django models are specifically to represent database schema.

Reading between the lines, perhaps you are looking for something not covered by a web framework? Django's features are for responding to HTTP requests, managing a relational database, auth, email, admin, templates etc. If you're trying to do something not part of this, use other parts of the Python language. Django is a library; Python is the more fundamental tool used to build applications.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: