by BiteCode_dev · 6y ago · 0 comments
They are meant to allow for the dev of an ecosystem.
This is why you have so many django plugins, and why they are so easy to integrate and play together.
This is why it's easy to have a dev envs, tests, signals and api generation with django.
It trades performances and flexibility for this. It's nothing more than a classic technical compromise.
You may need raw sql later on in your django project. But if you feel like starting with it, just go with flask. Django makes no sense.
You can even export all the contents of your DB to JSON or YAML and import it in Django, so you probably very well could go from SQLite to MySQL / Postgres if you really wanted to.
Edit:
The other actually funny thing is, Django doesn't force you to use its own ORM. You can write code that doesn't use its ORM. You can override anything and everything.
Yes! This functionality is extremely useful in a number of cases and isn't all that discoverable. At least in my experience, devs often don't learn about it until they've spent at least a bit of time working on a Django project. Once you get the (basic) syntax down it's quite handy.
You can, but what's the point using Django then? Everything is built around the ORM API, that's how admin is automatically generating pages, etc, etc. You lose the main advantage of Django if you don't use it.
Also the problem gets worst if you add to the mix something like elastic search. That it also has it's own schema and it's also has to be migrated..
With Django I always ended up hitting brick walls and ugly hacks. Whereas Sqlalchemy more easily fit in any use case that I had over the years, mainly because of it having multiple layers of abstraction so I can choose and mix as applicable. Learning curve is a bit steeper i admit.
I haven't used Sqlalchemy drop in replacement for Django ORM yet, but would consider it for a future project.
The only ORM that I have used that handled more complex stuff well was Jooq (Java). Having your database as the source of truth (and running a script to update Java Objects) works far better than the SQLAlchemy approach. I use Django now for a simple app but I used both Jooq and SQLAlchemy on something more complex (the issue was references to parts of the data models across components), and the former was brilliant (although learning curve) and the latter was just awful. I don't think SQLAlchemy docs are good either (again, started out as X and they just kept adding and adding).
That is just my experience. I used Django right now, there are no crazy cross-cutting concerns between components, and it works fine.
This separation is what I like about it, if I know how to do something in SQL I can use the tools sqla provides and it maps very close to 1:1. Or I can use a more ORM driven approach similar to what Django does. I can adapt it over time as the project evolves.
But yes to write the queries in Python and not raw strings, you need to have either a Table or ORM Model defined, which you could generate from the database [1]. But I admit, except for some scripts, I have never used it where Python wasn't the source of truth, so don't know what it all involves.
I don't know about very old versions of SQLAlchemy, but at least in the last 4.5 years since I'm using it, I have been much happier with it for medium sized projects over any other similar tool in Python I have tried because of this flexibility
[1] Edit: Or use reflection to load schema information from the database
>>> messages = Table('messages', meta, autoload=True, autoload_with=engine)
>>> [c.name for c in messages.columns]
['message_id', 'message_name', 'date']They optimise the wrong things and decrease security.
They're not meant to optimize for every edge case, they're meant to be as generic as sanely possible.