Earlier quoted context omitted.
The one thing I really appreciate with the ORM is that you really can get the ORM to make... more or less any sort of SQL query you want. It can take a while to wrap your head around what fields get used in aggregates and the like, but when working with big models with like 65 fields and juggling a bunch of stuff, not having to futz with serialization/deserialization and "just" expressing your problem in the dumb way…
> The one thing I really appreciate with the ORM is that you really can get the ORM to make... more or less any sort of SQL query you want. And if you can't make the ORM make the SQL query you want, you can just write it as a SQL query, like this godawful monstrosity: x = Site.objects.raw("select id, name, lat, lon, 111.045*degrees(acos(cos( \ radians(latpoint))*cos(radians(lat)) \ *cos(radians(lngpoint)-radians(lon)…
What I love about Django
101–110 of 120 posts
Re: What I love about Django
#102Earlier quoted context omitted.
I seriously don't get why Django ORM is using the Active Record pattern. This is such a stupid footgun that trivially causes horrible performance and BEGS you to cause n+1 problems. Never in my life did I have a problem with lazy loading causing unbearable performance until I joined a Python Django team. I really tried to find sympathy for the "dynamically typed" folks (please spare me saying Python is technically st…
> BEGS you to cause n+1 problems select_related, prefetch_related. n+1 problems be gone.
Then it got to where I had to make a reflective function that I use like Model.objects.defer(*all_fields_except(Model, ['field1', 'field2'])), and then add another all_fields_except() for every select_related and prefetch_related.
Even save() by default re-writes every single field. You have to use save(update_fields=['field1']) instead.
Re: What I love about Django
#103Re: What I love about Django
#104Earlier quoted context omitted.
I mean if you're doing it this way, you're really not applying best practices as a developer (never mind as a Django developer). > Models being passed around everywhere, queries happening everywhere. No, as a developer you still need to be 100% aware of the underlying queries and potential performance issues. No excuse for N+1 problems. ORM is not an excuse to be lazy, but I admit it will probably catch quite a few d…
Django allowing queries to be anywhere is more or less in line with Python’s overarching “we’re all consenting adults here” ethos. There’s probably one correct way to do it, but if you want to shoot yourself in the foot then here’s your gun. It definitely takes a bit of discipline. The key layers are somewhat easy to manage—middleware, context processors, views, template tags—but I’ve seen some hairy lasagne further…
It's highly productive if you do it right.
Re: What I love about Django
#105Re: What I love about Django
#106Earlier quoted context omitted.
You misunderstand. And that is exactly the problem. We did do that and that's why our queries ended up being several lines long. But if you missed just one model? You openly walk a knife again. It's a mess and it only gets longer and longer. I ended the project with having some proper aggregates, only for that to be thrown out of the window by the guy after me.
> our queries ended up being several lines long Which is... perfectly normal for non-trivial needs. > I ended the project with having some proper aggregates, only for that to be thrown out of the window by the guy after me. How is that a Django problem though ? Sounds like a skill issue on your successor. I get what you say, there's plenty of debates about ActiveRecord vs. AnythingElse, but in the end this one has it…
Re: What I love about Django
#107Nice. I've been meaning to do my own Django post, on some other bits we take for granted - I should do it. People should be using Django, the best parts are so useful you don't notice them until you switch platforms and implement them badly. Every app that used a more narrow solution ultimately ends up implementing parts of Django badly. The best way to solve this from Djangos side would be to have official ways of:…
Re: What I love about Django
#108Django while opinionated is very flexible too. unlike Rails. that means you can mold it to fit your use case easily - don't like the ORM - you can plug SQLalchemy and use a different 'architecture'. + you can use multiple different databases if you think that's the right path. in Django there's no 'the rails way' - you choose your own path. Django-admin by itself saves so much work specially If you're doing B2B stuff…
The Rails Way is a response to people changing too much Rails and making it super custom.
In the last 4 years alone I touched codebases ranging from Rails Way to Rails and everything dry.rb, to Rails and Grape and Sorbet, to Rails and service objects everywhere and a lot more combinations.
Re: What I love about Django
#109Earlier quoted context omitted.
> The one thing I really appreciate with the ORM is that you really can get the ORM to make... more or less any sort of SQL query you want. And if you can't make the ORM make the SQL query you want, you can just write it as a SQL query, like this godawful monstrosity: x = Site.objects.raw("select id, name, lat, lon, 111.045*degrees(acos(cos( \ radians(latpoint))*cos(radians(lat)) \ *cos(radians(lngpoint)-radians(lon)…
I'm not seeing anything that can't be done here without using raw() though?
How would you have approached it?
Re: What I love about Django
#110Earlier quoted context omitted.
> BEGS you to cause n+1 problems select_related, prefetch_related. n+1 problems be gone.
You misunderstand. And that is exactly the problem. We did do that and that's why our queries ended up being several lines long. But if you missed just one model? You openly walk a knife again. It's a mess and it only gets longer and longer. I ended the project with having some proper aggregates, only for that to be thrown out of the window by the guy after me.
https://docs.djangoproject.com/en/dev/releases/6.1/#model-fi...
That's the great thing about Django, it's been around so long and the quality bar is so high that eventually all the major rough edges get sanded away usually in a really well considered manner.