Django REST framework 3.5
django-rest-framework.org
django-rest-framework.org
* ModelViewSet is read/write, you should use the more verbose ReadOnlyModelViewSet until you know you want to allow writing.
* Fields specified in the "fields" member are read/write by default. You have to explicitly declare the field on the ViewSet and pass "readonly=true" to make it read-only. This is especially dangerous for ForeignKey fields, which can be used to change object ownership if you aren't careful.
When I was responsible for a DRF-based API I wrote some custom Fields and ViewSets to use safer defaults, and I recommend others do the same.
Example:
class FooViewset(
mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.DestroyModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet)
Subtract as necessary. url(
r'path/$',
TheModelViewSetView.as_view({"get": "list"}),
name="thename"
)
or "retrieve" instead of "list" for a route which includes PK.This of course makes the entire path read-only so it's not a way to make some fields writable and others not.
Completely agree. But DRF is useful even if you don't use any of the serializers. E.g. you can just use it to handle auth, requests, and responses, and then validate things by hand and save them directly to the Django model. You still pretty much need serializers when validating image files because you can't trigger the Django code to do this by saving to the model directly, but for everything else it's often more readable just to serialize and deserialize by hand.
DRF scores well here. It's designed in an intuitive way. The healthy community around it is a big plus as well. Can't count the number of times I've come across a SO article that had updated answers for the latest versions.
I only wish there was a way to make one time donations. Currently you have to sign up for a recurring plan.
It's also important to measure the likelihood to have to find an answer in the first place. There are vibrant communities around many frameworks, but it doesn't make them all good.
Speaking as someone who went from DRF -> Rails -> back to DRF, I can say that DRF is sufficiently confusing to understand and get started with. The problem partially exists within Django itself, but DRF doesn't exactly help. It can feel like pulling teeth to create a simple API with DRF. And, as the another HN comment explains, DRF does a ton of "opt-in" work that can end up doing more than you intended.
My biggest gripe with DRF is how coupled it is to the Django ORM. All of the nice ViewSets are only useful if you have cookie-cutter Django models, but we all end up changing them.
I tend to enjoy working with Ruby Grape APIs:
https://github.com/ruby-grape/grape#basic-usage
No need to deal with 100 types of ViewSets, Serializers, Renderers, Parsers, and so on. You just need to understand that data is being passed back and forth. If you need something more complicated, just add it yourself.
I wish Python had something similar. Something that's between Django & Flask in terms of complexity.
Eve: https://github.com/nicolaiarocci/eve Hug: https://github.com/timothycrosley/hug flask-restful: https://github.com/flask-restful/flask-restful
I can't wait to see support for real-time views. Is that in the cards for DRF 3.6?
drf is usually compared to sinatra.
I see Sinatra as more similar to Flask than DRF.
All the new schema generation functionality is interesting. It's probably just a matter of time before someone builds a tool that reads the schema generated by Django, and syncs the Ember models with it. That's currently one of the drawbacks of using separate frameworks (and languages) for the client and server.
For a new project where we were starting from scratch, we stuck with Ember Django Adapter.
You wouldn't need to think about promises or callbacks at first place, which would make things way easier. That's the main reason I gave up NodeJS for Go when working with APIs. Async programming is noise. CSP is a better paradigm.
[1]: http://doc.akka.io/docs/akka/2.4/general/actor-systems.html#...
Maybe the DRF for Ruby is grape https://github.com/ruby-grape/grape/blob/master/README.md but I never used it. I just fill in the standard scaffolded REST methods in the controllers with queries and write json views (Rails and Django have different terms and concepts, MVC vs MVT.)
I find it's Model centric approach a whole lot nicer to work with then Django-Rest .
When you publish internals, you only cement your implementation so you can't change that easily later (e.g. when you realize how crappy it was at first), and you even need to pay for that drawback with ease of use of your API (API's user would need to understand the system he talks with). It's a lose-lose deal.
(A slight annoyance I've found here: the CoreAPI schema spec doesn't seem to have a place to attach a top-level summary of the API, which is useful as a 'man page' for the rest of the API calls. Swagger does allow for that.)
Is it a good practice to handle all verbs in the function dealing with the particular request as shown in the example?
I think for a real world scenario that would be a mess to read, but for a less experienced developer this could hint that such approach is alright and result in a less readable code base in the future.
Basically, you will 1. split verbs to different methods of one class and 2. you can not implement verbs you are not going to allow (you can of course do that also with the functional style you see in the link you posted)
values = User.objects.all().values('id', 'username')
results = json.dumps(values)
…or use the builtin JSON serializer. You can also use Paginator to paginate the results.
However, I still think that Django should have something like the "JSONResponse mixin example"[1] built-in by default; sometimes you are building a product which is not an API but you still need the occasional RESTful endpoint and, in those cases, something like DRF would be overkill.
[1] https://docs.djangoproject.com/en/1.10/topics/class-based-vi...
Django does plan on bringing DRF's content negotiation into the main framework:
https://www.djangoproject.com/weblog/2015/dec/11/django-awar...