17 ms·
Django REST framework 3.7 released
- doughj3 9y agoInteresting, I see that Bayer is the sponsor of this release but I can't find any open positions on their careers site with a keyword 'Django'.
- tolgahanuzun 9y agoReally interesting. At first I did not understand. :)
- stephaner 9y agoDo you want to work for Bayer to help the company to destroy the remaining life on earth?
- ndanmand 9y agoWould you use this instead of Flask? Why/why not? I'm planning on learning Flask, but would love any thoughts on this from experienced users.
- andreagrandi 9y agoIf you already use Django and want to expose an API, DRF is a natural choice. Flask is more simple to use, if you have to do simple things. If you need models, access to a database and you don't want to pull third party libraries for things that are normally part of a complete framework, I usually suggest to use Django rather then Flask. This said, Flask remains a nice, light framework and I personally use it for a few services.
- minimaxir 9y agoDepends on what you want to do. Django’s ORM lets you do many things which would take awhile to reimplement in Flask.
- yilugurlu 9y agoCheckout Apistar then and see Flask's capabilities. https://github.com/encode/apistar https://github.com/encode/apistar
- rozenmd 9y agoHaving used apistar in production for a month now, I sort of wish I went with Flask...
- yilugurlu 9y agoWhat's the catch?
- wkd 9y agoI've used apistar quite extensively in two different projects (one launched) and so far I like it. One disadvantage is you can't use wsgi middleware, but after rewriting our middleware to components they ended up being much neater. I'm a fan of the bottom up approach where you define the components you want in a route. We have some routes which just returns the database query and with a custom renderer it will get rendered in the json format we expect
- aidos 9y agoThat's definitely wrong. If you're using Flask and need an ORM you'll use sqlalchemy - which, in my experience, is the best ORM in existence (for any language).
- oneweekwonder 9y agosqlalchemy the reason Im in love with python! edit: To make this a more-effort post see https://www.sqlalchemy.org/features.html https://www.sqlalchemy.org/features.html and http://www.aosabook.org/en/sqlalchemy.html http://www.aosabook.org/en/sqlalchemy.html for reasons.
- 9y ago
- theptip 9y agoThe Django ORM and DRF autogenerated views are very good for quickly building CRUD APIs. You get a lot of stuff in the box with Django: * a very functional (if exceptionally complicated and hard-to-modify) admin UI * a friendly ORM with good support for autogenerated DB schema migrations (better than SQLAlchemy for simple CRUD, but harder to go off the beaten track when you need to) And building on that, DRF gives you * Autogenerated CRUD API endpoints for your DB models * Autogenerated serializers for your DB models, with field type validation All of the above exists as plugins for Flask, but the feature set is less well integrated, and you need to figure out which plugin you want to include. If you're trying to build a super-light microservice then Flask is probably worth looking at, though you can get Django quite light with a bit of work. If you're trying to build a more complex system with nested API endpoints then DRF makes this harder for you (it's not possible in native DRF, but there's a plugin for it; I'm using this approach so it's not terrible). And if you don't buy in to Django's ActiveRecord ORM approach (e.g. you want to do DDD and want to use a DataMapper style interface on your DB), then you need SQLAlchemy, and you can't use all the clever ORM mapping tools that Django/DRF brings, and so you may as well use Flask.
- Daishiman 9y ago> a very functional (if exceptionally complicated and hard-to-modify) admin UI The flexibility of the admin UI has improved dramatically in the last few releases. It's still relatively complicated, but it now has way more places to override behavior.
- andybak 9y ago> last few releases By "last few" you mean "since about Django 1.4" in 2012
- andybak 9y ago> exceptionally complicated and hard-to-modify As an exceptionally mediocre programmer who routinely creates extensive admin customisations I dispute this. Seriously. Where does this meme come from? The admin is remarkably easy to customize for the most part. It's biggest flaws are incomplete documentation and a few monolithic methods/templates mostly around changelists.
- ergo14 9y agoCconsider also looking at Pyramid Web Framework. Provides nicer defaults and does not rely on threadlocals and globals. By defualt you will also get sqlalchemy and jinja2 templating out-of-the-box. It also handles building REST apps nicely. Django on other hand will give you monolithic solution for the price of less flexibility than flask/pyramid. If you just want to handle REST views - I think falcon framework is nice (speed wise) - haven't used it myself yet though.
- antod 9y agoPyramid also has some REST specific addons available for streamlining development - eg Cornice etc
- aidos 9y agoI looked into Pyramid but couldn't find an active admin project. Is there one that people tend to use?
- ergo14 9y agoYou mean like auto-generated admin panel? I would look for something directly related with sqlalchemy - not pyramid. I don't use such panels so I can't give good advice here.
- oneweekwonder 9y agoIf your interested in flask and api's have a look at https://github.com/zalando/connexion https://github.com/zalando/connexion, but personally I'm more a bottle kind of guy.
- askvictor 9y agoFlask gets you up and running extremely quickly, but as soon as you need to do anything moderately complex you need a heap of extra packages or code. Django comes with the kitchen sink included, but has a much steeper learning curve to understand how all of the parts for together (e.g. there may be 3 or 4 files you need to edit to make a change to something). I use both, for very different applications. There are also some packages for Django that make it more flask-like (e.g. importd) but I've never used them in real life.
- spamizbad 9y agoI recently used it to implement a BFF (backend for front-end) for a mobile app on a "legacy' system. I spent about 3 normal, non-crunch days to code and write ~20 endpoints (most with 2-4 verbs) w/validation and tests that spanned over 25 models. Also had about a dozen context-sensitive permissions to account for as well in a DRY manner (eg: user must be of type X and have plan Y or return a 403). Normally not a "big" framework kinda dev but I'm quite proud of how clean and well organized the resulting code was. Adding additional endpoints in the future can be done in minutes or hours at most, since all the pieces are now there. I don't have any experience beyond that though, and I've never started a brand new product from the ground up with it, but it made my life super easy last week.
- peller 9y agoI recently went down this path, trying to build something akin to DRF but using Flask. I'd never used Flask before, but had some Django experience and was no stranger to Python or web frameworks in general. With Flask there are a lot of choices you need to research/make around which libraries to use, and how to get them all playing nice together. I obviously ended up using SQLAlchemy but decided to spend some time writing a customized integration between Flask-RESTful and Flask-Marshmallow for the views/serialization layer. Certainly it took me a bit longer to get up and running compared to if I'd just used DRF, but on the plus side, I feel like I have much stronger grasp on how everything works now, whereas Django still has a ton of surface area that feels a bit like magic to me. If you're interested, the repo is open sourced under MIT here[0]. [0] https://github.com/briancappello/flask-react-spa https://github.com/briancappello/flask-react-spa
- c0un7d0wn 9y agoI went the other way around and came up with https://github.com/shosca/django-rest-witchcraft https://github.com/shosca/django-rest-witchcraft as the project is already using django+sqlalchemy and I needed to do api's on existing things.
- peller 9y agoNice! How do you find SQLAlchemy integrates with Django overall? (Can you use its models with the admin?)
- nerdwaller 9y agoFlask is great, but it's bring-your-own-*. Meaning that you need to pull in everything custom and, in many cases, that means you re-implement something like django in a different way. Each has their own merits, and being a Python developer knowing both is often good. Granted I see a lot more job postings for Django than Flask, for what it's worth.
- sixhobbits 9y agoI normally recommend that people learn Flask if they really want to learn. Flask is simple and you have to do more yourself, which means you really get to understand how each component works. Django will get you set up faster, and has a lot of third-party middleware that you can use off-the-shelf. If you're learning, some of it will seem like magic and it'll take longer to figure out what does what. TL;DR - Flask for education and simple projects. Django for creating CRUD apps where you just need it to work. Shameless plug -- I wrote this comparison[0] which shows how "Hello, World" is written in Flask and Django, and goes over some pros and cons of each in more detail. The editors added the "Why Flask might be better" part to the title, which caused most of the discussion about the post to turn into a flame war, but I tried to be as objective as possible (although I do prefer Flask -- another shameless plug for my book[1] Flask by Example). [0] https://www.codementor.io/garethdwyer/flask-vs-django-why-flask-might-be-better-4xs7mdf8v https://www.codementor.io/garethdwyer/flask-vs-django-why-fl... [1] https://www.packtpub.com/web-development/flask-example https://www.packtpub.com/web-development/flask-example
- polote 9y agoI disagree, I don't think Flask is better for learning than Django, It is true that many components are already built inside django but the complexity is not really hidden, the developers still need to understand how the framework works. For sure you can create a basic view with a ModelSerializer by hiding all the complexity, but very often it won't be enough to build what you want to build, and therefore you will have to search trough the documentation how things works. In my opinion both Flask and Django are good for learning purposes, but Flask requires the user to handle more complexity than Django, which is not always the best thing if you want to learn
- mplewis 9y agoI think one of the big reasons to try Flask first is to learn what magic is under the hood. If you start with Django, you're asked to build your MTV in a very specific way. That way Django can give you stuff like XSS/CSRF protection. If you don't know what magic Django is doing for you, it feels restrictive to do stuff the Django way.
- aidos 9y agoLots of advice on here but I'll try to add something useful. For a project, you can't blanket say "use flask" or "use django with DRF" without a little more context. They're not even that comparable in a way. In terms of learning, if you already know how to code then just take a quick look at Flask. The core library is so small you can get the gist of it in less than an hour; there's nothing to lose. That's the great thing about Flask. After that you'll be fairly well on your own. There are libraries for most everything you need to do but you'll have to hack on them yourself. If you have no dev experience then get a page running with flask just to get you excited about development...and then maybe stop there. You have to weigh up too many things and figure out how to get them all working nicely together to do more complex things. Django can guide you through this process better with the decisions made for you. DRF is a really nice library. In fact, as someone sworn off django from the bad old (v0.9) days, I almost went back because DRF is so nice, and that was back when it was DRF v1. Tom Christie is an brilliant developer and if you're building an api, DRF is best of breed.
- dec0dedab0de 9y agoFlask is better if you want to do more of the work yourself.
- minimaxir 9y agoNote that v3.7 was released a month ago, however it’s been awhile since DRF has been on HN.
- dopeboy 9y agoBig fan of DRF. Recently signed up to become an individual sponsor. Does anyone know how DRF works with channels? What is the process to integrate real time support into an existing DRF app?
- trowawee 9y agoIt's pretty easily integrated. The big pain point will probably be moving to an ASGI server like Daphne.
- ftxrcc 9y agoHi, running this in production for a startup (taleship.me). We use Channels + DRF more recently. It works great, no problems there, and Daphne doesn't interfere at all with it.
- sgt 9y agoI should do that as well. I think DRF is probably pulling about $8k/month through the sponsors so they are really important for the project.
- mattgreenrocks 9y agoI looked at DRF for a CRUD site but it didn’t seem to add much over Django for HTML-based apps. Am I missing something? Relatedly, I find working in Django much more sane than Rails, even though I am way more familiar with the latter. Maybe I don’t hate web dev completely :).
- lilbobbytables 9y agoDRF is a framework for building a REST API. So, not helpful for an HTML based app that doesn't also require you to have a REST API.
- cies 9y agoHTML is for UIs (primarily for human consumption), DRF is for APIs (primarily for machine consumption).
- throwaway13337 9y agoI enjoyed working with drf for a project but found the lack of automatic nesting support odd. Is full nesting support on the road map or is there some fundamental problem with it that they don't want to touch?
- iliaznk 9y agoYou must be talking about nested URLs? I faced that problem too and then realized that you don't need any special support for that (at least if your case is not very complicated), in our project we just use URL kwargs for a viewset like this: `/users/(?P<model_pk>[0-9]+)/comments/` and then just get the required object in `def get_queryset` or throw a 404.
- lilbobbytables 9y agoWhat do you mean by automatic nesting? You can easily add a serializer as a field on another serializer to serialize nested objects.
- amorphic 9y agoSeveral people have mentioned Flask. Eve is a well-written and actively-developed Flask framework for building RESTful API services: http://python-eve.org http://python-eve.org. It's most at home with a MongoDB backend but can also use SQL RDBMS, ElasticSearch and Neo4J.
- stadeschuldt 9y agoWhat's the benefit over Flask (which is part of Eve anyhow)?
- tixocloud 9y agoIncredible fan of DRF and I really wish to thank all the contributors for the amazing effort in making this framework so simple to use.
- astrix_pre 9y agoI don't think Flask is better for learning than Django