15 ms·
Django 3.1
- d33 6y agoPardon the rant, but I feel that the advantages don't outweigh the fact that with each release some of my stuff gets broken and I need to adjust. Django puts DeprecationWarnings basically everywhere and it's a hell to maintain projects that had been alive for a few years. God forbid you do anything with the interfaces they expose. The problem is only getting worse when you consider your dependencies, which in many cases don't keep up with the release cycle of Django. It's a mess.
- rbanffy 6y agoI tend to keep one CI pipeline with all dependencies unpinned (I made pip-chill for that reason) and Django set to the latest version to get early warnings of the impending doom. That said, it takes a while for the Django folk to end support for a release so, at least, there is not much of a hurry to update a running app.
- orf 6y agoCan you elaborate on some of the things you've been frustrated with? I find Django has a pretty solid deprecation policy, with the goal being that if you have no deprecation warnings then upgrading is simple. Third party dependencies do get in the way of that sometimes though.
- metafunctor 6y agoMy experience has been good, with a Django app maintained for over 8 years now. We've only updated Django when a new LTS version is out. That means once every two years there's an update that may break something, but I don't remember it ever being more than a few hours of work. We do take some care to (mostly) only use documented interfaces, so maybe that helps?
- danpalmer 6y ago> Django puts DeprecationWarnings basically everywhere This seems like exactly the way that deprecations should be handled. Are you suggesting they should maintain backwards compatibility for much longer? They already have a pretty long deprecation policy.
- djaychela 6y agoI'm only a beginner with Django, but I tend to agree to a point - reason being is that I've found old code that I want to use (github, etc.), but it targets Django 1.x (later versions, say 1.10), and will use a load of stuff which is now removed. The issue I've had is that the documentation seems to be difficult to find these removed elements - such as last week, looking for the {% ssi (file) %} tag, which it took me the best part of an hour searching to find out which version it came from - and indeed that it was a standard tag! A lot of the imports seem to move around (which is easy to fix, once you know where they now are), and some things have just vanished. I know documentation is a nightmare to produce, but there must be a way to automatically produce it for either import relocations or deprecations so you can find things like this easily?
- Daishiman 6y agoSorry, but Django is the best framework I've found for managing deprecations. In the web world it's quite frequent to have major versions every couple years that break everything and require substantial dev cycles to keep up. That said, 1.x applications are quite old by now.
- Alex3917 6y agoFor me, upgrading Django for a series-A sized startup (e.g. 25KLOC) takes maybe 2 - 4 hours on average, assuming there is good integration test coverage. What parts of the upgrade process are you finding take a long time? I feel like the last Django release that was at all complicated to upgrade to was maybe 1.9, in terms of getting the test cases to run properly in parallel if they hadn't been properly isolated. And even that was more of a Python3-like situation, where it really only exposed things that people had done incorrectly previously.
- WD-42 6y agoI'm sorry, but this is just programming. You have two choices: 1. Never upgrade and keep everything stable but go through hell when you inevitably must update something or 2: keep up with the latest and go through some minor pain with every release. It's a lot like taking care of a house or car. The world does not stand still, much less programming frameworks.
- zelphirkalt 6y agoHmmm, with well chosen, thought through abstractions, which only presume the minimum they need to presume about their usage, the frequent changes you describe are not a given. The question is, what it is, what frequently needs to change and why no appropriate abstraction has been found to reach stability.
- WD-42 6y agoWe're talking about a huge web framework here. Your comment might apply to small isolated pieces of code. But Django is built on top of thousands if not millions of abstractions. It's absolutely unreasonable to think that it won't change.
- neurostimulant 6y agoCompared to how fast things move in frontend world, Django feel like ultra stable to me. Upgrading an old django project to the latest version typically doesn't affect any fundamental things. Maybe some classes has been moved into contrib or spin off as non-core packages, or some methods has been removed and replaced with another methods, but never fundamental changes that requires you to rewrite majority of your code.
- deleted 6y ago[deleted]
- Jonnax 6y agoCool, they've implemented async views. ASGI has kinda passed me by. For some small project's I use Gunicorn. What's the setup for ASGI that's popular?
- iaml 6y agoThey support Daphne/uvicorn https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ I think in python world uvicorn is more popular choice, might be wrong tho
- neillyons 6y agoYou can use Uvicorn to run ASGI apps. You can also use it together with Gunicorn https://www.uvicorn.org/#running-with-gunicorn https://www.uvicorn.org/#running-with-gunicorn.
- laactech 6y agoI've been running uvicorn with gunicorn in production, and it's been solid. Django 3.0 enabled this with ASGI support. It'll still be a while before django is fully async capable, but these are steps in the right direction.
- simonw 6y agoI've been using Uvicorn for a couple of years on a whole bunch of projects and it's not caused me any trouble at all.
- VWWHFSfQ 6y agoGunicorn with the Uvicorn worker class is the recommended production solution
- konschubert 6y agoSlightly OT: How does HN feel about the recent craze to make web python asynchronous? To me, the performance gains are dubious in many cases and the complexity overhead of handling cooperative multitasking just seems like a step back for a language like python.
- d33 6y agoI feel like it's too little too late, but the idea is good. There's an obligatory reading: https://news.ycombinator.com/item?id=23218782 https://news.ycombinator.com/item?id=23218782 Five times later, there are some new frameworks, but much of the ecosystem is still sync-only. This is actually one of the things that is pushing me towards Go lately. Python just doesn't seem to mature fast enough and tools heavily disagree on conventions.
- whouweling 6y agoWell, if you have external API calls in your Django app and you are running sync (which I would absolutely advice, with running async it is really easy to get an unpredictable performance which is sometimes hard to track down) having the ability to run some views async is really crucial. Otherwise your application might me humming along smoothly at some point and coming to a sudden complete standstill or performance plummets when a random external API endpoint starts to time out. Yes I have been bitten by this :-) To fix this while running sync I have dedicated separate application processes for the views that do external calls, but this makes the routing complex. Alternatively you can juggle timeouts on the external API calls but this is hard to get right and you need to constantly keep track if calls are not timed out just because the external endpoint is a bit slower at some point. So I think this solves a very real-world challenge.
- viraptor 6y ago> plummets when a random external API endpoint starts to time out You should add something like https://pypi.org/project/circuitbreaker/ https://pypi.org/project/circuitbreaker/ Continuously failing external requests should not make each one of your responses slow.
- scrollaway 6y agoI've been using 3.1 alpha/beta for a while and it's got some pretty nice improvements. "JSONField for all databases" is a huge deal IMO, as it simplifies a lot of fairly common use cases.
- grenoire 6y agoJSONField has been an absolute godsend in combination with Django's ORM. I had been using it with Postgres and will likely keep our backend the same, but I cannot recommend it enough. You will have to write some validation and schema code on top if you want your data to have similar (but weaker) guarantees to the usual typed fields; the benefits from the flexibility you get are immeasurable though.
- nojs 6y agoCan you give some examples where a relational schema isn’t suitable?
- kumarvvr 6y agoOT Question : When choosing a stack for creating sites, would you choose Django over ASP.NET Core or PHP? If so, why?
- favadi 6y agoYou only know python.
- kumarvvr 6y agoI am good with Python and C#
- jpgvm 6y agoThen ASP.NET is probably the superior choice. C# is a better language by far and the .NET runtime is a better runtime by far. It only ever makes sense to use Python for data-science one-offs and munging if you have competency in Java/C#/etc. It's not a language well suited to application development.
- snypox 6y agoDon’t understand why you got downvoted. I work in a Python shop and 100% agree with you.
- nix23 6y agoBecause he say's a hammer is far better then a screwdriver, and Metal is far superior to wood.
- acdha 6y agoLook at the number of sweeping absolutes presenting their opinion as a global truth, with no supporting evidence presented. Threads which start like that rarely end up contributing anything of value or convincing anyone who didn’t already agree with the author.
- 6y ago
- waihtis 6y agoI'm closer to a hobbyist than a professional dev, but the async views seem like a big functionality. Having done some Django apps, getting a synced up view for some changing variable was always a bit painful.
- StavrosK 6y agoI'm not sure you're talking about the same thing, this is about asyncio/green threads.
- waihtis 6y agore-read the documentation but I'm not following. This example: async def current_datetime(request): now = datetime.datetime.now() html = '<html><body>It is now %s.</body></html>' % now return HttpResponse(html) seems like an example that I was thinking of.
- StavrosK 6y agoI don't know what you're thinking of, unfortunately, but that piece of code just returns the current date when you visit the page.
- FreeHugs 6y agoWhat would be a typical use case for "Asynchronous views"?
- nurettin 6y agoit would be useful if your code has to execute async code and await it's result from within a view. For example: create 10 tasks and return await asyncio.gather(*tasks) as json.
- FreeHugs 6y agoI don't know much about Python's async tools, but why couldn't that be done in a non-async view? In pseudo code I would think it would look like this: def my_view(request): name = async_get_name() // returns a promise city = async_get_city() // returns a promise waitUntilResolved(name,city) return HttpResponse('Hello '+name+' from '+city)
- leetrout 6y agoI hope they prioritize some support for ROLLUP and friends. I’d never used it before and it was fantastic but I had to drop down to raw sql to do it. SQLAlchemy has had support for well beyond a year. I’ve used Django since 2008 and I love it with all its warts but I’ve really grown to prefer SQLAlchemy.
- jsmeaton 6y agoAdding new SQL support to the Django ORM is tricky. There isn’t a nice low level abstraction for generating and composing SQL. It’s mostly a bunch of lists containing different kinds of data (strings, Col, Expression) and they don’t compose well. On top of that, you’d need to come up with a decent frontend syntax that aligned with the existing methods. I think Django made a mistake when first defining the language of aggregates by overloading the values() method and not using group(). To support rollup, values() would need to support it but only when used after an annotate. Not nice. I often think about what it’d take to use alchemy for the sql building beneath the Django frontend. That would open up so many more possibilities and features.
- leetrout 6y agoYea I started to bring up the existing ticket for grouping on the dev list and ask what people thought about it. I also ended up writing a very tiny transformer function and using that directly because core only has a couple supported casts and I needed Postgres timestamp types so I could extract and rollup on the year / month / day. That gave me some insight in to some of the patterns in use in “lower level” Django differ from the expressiveness / composability of SQLAlchemy.
- true_religion 6y agoSQLAlchemy has a great architecture, but it comes at the expense of being tied to the use case of accessing a relational database. Django models are more abstract and anemic in querying, but it means you can use the same API and write access layers for non SQL databases. At work, we have an in memory database, and Elastic Search all queried in an identical way to the Postgres models.
- jtdev 6y agoDoes the ORM still write horrendous cursor based loops where simple joins would suffice?
- stefano 6y agoBy default yes, but you can configure queries fetching behavior using select_related/prefetch_related to avoid the N+1 queries problem.
- collyw 6y agoSadly not so many people know how to use those. From the Django projects that I have inherited at least.
- selectnull 6y agoDjango ORM mainly hides the SQL from the developer (which is kind of the idea of any ORM). But if a developer does not understand the underlying SQL concepts, they will soon write performance wise horrific code. But, if you remove the ORM from that equation, I don't see how that same developer doesn't make the same mistakes. So in the end, I don't think that Django ORM (in its core) can help much in this area. There are of course tools that can help with that (django-debugger and others).
- yebyen 6y agoThis is a hot-take from Aaron Patterson, Rails and Ruby core team dev, in his keynote this year where he addressed a very similar idea on a perhaps related query generation topic... https://youtu.be/9JEEabL90AA?t=1360 https://youtu.be/9JEEabL90AA?t=1360 To give you the tl;dr (he goes through profiling and a great deal of data to help show what a core dev needs to do in order to help us solve this one specific case, and...) Aaron comes to the conclusion that there is a "performance pro-tip" which many Rails devs have learned, similar to what y'all are discussing in this thread for Django, and in this one specific case he outlines in some detail, Aaron considers that there is a bug where the programmer needs to know this "pro-tip." As there was really no reasonable way for the Rails engine to interpret this instruction from the programmer as to mean "do it the slow way" when there's a better way to do the same thing with no drawbacks, and it would have been possible for the engine to safely do the smarter thing (it was IIRC to precompile some better-shaped SQL, bringing back the same result set in fewer queries, (making up the most runtime performance by spending less time in the SQL compiler overall, in the worst performing pathological case, all this is shown with data)...) and that having this behavior here is actually much better for a novice programmer's behalf, in order to make that better performance happen without requiring a developer to manually build such hints into the application code, at abstraction layers where they really shouldn't have to be thinking about those things anyway.
- sammorrowdrums 6y agoI'm surprised the discussion has derailed into whether or not to use JSON fields... Certainly you don't have to, but they've been in Postgres contrib for a long time. Async views have dropped, and that is genuinely exciting. It's taken a lot of work, and there's still a ways to go before Django is truly async native, but this is one of the big steps, and being able to do some concurrent IO on a few routes could be a huge saving for some projects that call out. Otherwise a lot of stuff has to be done in event queues, to avoid blocking the main thread, and sometimes that means a bad UX when users take actions and aren't offered the guarantee that they are complete - in times where that might be the best option, were you not risking blocking thread.
- btown 6y agoAs an FYI: Using a gevent monkey patch has been a way to get async Django for years. Overhead is inefficient in CPU cycles and you need to stay away from doing CPU bound things like Numpy manipulations, but for an app server that’s bound by external API call latency, it practically gives infinite concurrency compared to a thread-per-request model. And no need to worry about event queues. You can feel free to synchronously call requests.get with a 10 sec timeout and still serve prod traffic from a small handful of threads. And most days we don’t even worry about async from a coding perspective. Anyone can email me with questions.
- mgraczyk 6y agoI'd add that crucially, DB ORM operations just work with gevent. It will be a while before async database operations are supported natively by Django. For me that is a complete blocker.
- georgewalker355 6y ago> DB ORM operations just work with gevent My impression was that django+gevent requires some care for the db part. The psycopg2 docs state, for instance "Psycopg connections are not green thread safe and can’t be used concurrently by different green threads." [1,2]. In addition, gevent cannot monkey patch psycopg2 code because it is C and not python. This is handled by calling psycopg2.extensions.set_wait_callback() [1,3,4] I just now realize that you're probably referring to making the ORM itself async capable which is on the roadmap https://code.djangoproject.com/wiki/AsyncProject https://code.djangoproject.com/wiki/AsyncProject in which case I totally agree. [1] https://www.psycopg.org/docs/advanced.html#support-for-coroutine-libraries https://www.psycopg.org/docs/advanced.html#support-for-corou... [2] https://stackoverflow.com/questions/12650048/how-can-i-pool-connections-using-psycopg-and-gevent https://stackoverflow.com/questions/12650048/how-can-i-pool-... [3] https://github.com/gevent/gevent/blob/master/examples/psycopg2_pool.py https://github.com/gevent/gevent/blob/master/examples/psycop... [4] https://github.com/psycopg/psycogreen https://github.com/psycopg/psycogreen
- jungletime 6y agoAs someone that has an active app still written in Django 1.6. The larger my project and more complicate the more I wanted to ditch the model/view/template separation. And attach methods to the model so that it can be called from everywhere, returning html code in a string directly. Other times, I wished Django had something analogous to a component, where everything is just encapsulated in a single file. I don't want to separate javascript/html/css view/template. I want a single file I need to update and an easy way to call to render. The template system is also difficult to use, if you get complicated scenarios. I needed to show event details in a modal if that was the user preference. But the page could also be embedded. This lead to me having to render differently depending on the device, whether it was embedded and if it was a popup or not. This lead to an explosion of possibilities, a total 2x2x2 = 8 different views for the same data in the same template. The most practical way was with if / then statements but that still lead to me repeating html code. And being difficult to reason about and test. I also got into situation where the template wasn't parsed and updated. Probably because of too many includes or inheritance in the template. For example, I wanted to write a custom css file that would use google fonts that the user selected in the UI. The only way I found to work was to bypass the template and write a function in the view that would render_to_string the css file.
- mixmastamyk 6y ago> I don't want to separate javascript/html/css view/template. I want a single file This was always possible.
- canada_dry 6y ago>> I want a single file > This was always possible. Any good (code/github) examples of this??
- mixmastamyk 6y ago<body> <style>...</style> <script>...</script> … </body>
- 6y ago
- jungletime 6y agoSomething I wish that Django did was user defined functions in the template. It has for loops, which is good but it forces you to write the html in top to bottom procedural manner. It would be far better to be able to define a function that you can call for bits of html code that might repeat in the same template. Since I stopped using Django at 1.6 does the new version let you define functions in the templates?
- __alexs 6y agoIt has blocks for this https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-extends https://docs.djangoproject.com/en/3.0/ref/templates/builtins...
- jungletime 6y agoThat isn't as nice to having a function inside the same file you can call over, IMHO. Also lets say you define a block for rendering a button with a different color. Then use 20 different buttons on one page. Does that mean that the block code would need to be loaded from a file 20 times and parsed each time. Seems like huge performance hit to do it that way. Is there a technical reason why you can't have a function in the same file. Am I misunderstanding how it works?
- drcongo 6y agoDjango has full Jinja support these days which is vastly preferable.
- jungletime 6y agoCan you write functions in Jinja?
- mrigor 6y agoyeah, it supports macros
- 6y ago
- samirsd 6y agocan the async functionality replace celery?
- heliodor 6y agoNo. Celery can execute tasks after your view returns. Async can't do that.
- bredren 6y agoWould you please elaborate? If all of a task is encapsulated in a django async view, would this not be the same thing?
- heliodor 6y agoTo run async code, you use 'await' in python. That right there tells you that you have to wait for it to finish. You want to receive the result. The view will wait for a result before returning the http response. Celery and all task queues are fire-and-forget. You tell it to do something for which you don't want the result, so you can continue executing your code right away. The task queue can even take an hour to process, for example. Or you can schedule it to run later.
- samirsd 6y agothanks
- ram_rar 6y agoI wish, there is something equivalent like this in golang world. Django has been one of the best well maintained open source projects.