Adding REST to Django [closed]

2019-01-12 18:10发布

I've got a Django application that works nicely. I'm adding REST services. I'm looking for some additional input on my REST strategy.

Here are some examples of things I'm wringing my hands over.

  • Right now, I'm using the Django-REST API with a pile of patches.
  • I'm thinking of falling back to simply writing view functions in Django that return JSON results.
  • I can also see filtering the REST requests in Apache and routing them to a separate, non-Django server instance.

Please nominate one approach per answer so we can vote them up or down.

11条回答
我命由我不由天
2楼-- · 2019-01-12 18:50

Scrap the Django REST api and come up with your own open source project that others can contribute to. I would be willing to contribute. I have some code that is based on the forms api to do REST.

查看更多
女痞
3楼-- · 2019-01-12 18:50

I'm thinking of falling back to simply writing view functions in Django that return JSON results.

I would go with that ..
Ali A summed it pretty well.

The main point for me is beign explicit. I would avoid using a function that automatically converts an object into json, what if the object has a reference to a user and somehow the password (even if it's hashed) go into the json snippit?

查看更多
老娘就宠你
4楼-- · 2019-01-12 18:51

Tastypie is also a newly emerging REST framework for Django. It has the same mindset as pistons, and removes a lot of boilerplate coding.

查看更多
够拽才男人
5楼-- · 2019-01-12 18:52

You could take look at django-dynamicresponse, which is a lightweight framework for adding REST API with JSON to your Django applications.

It requires minimal changes to add API support to existing Django apps, and makes it straight-forward to build-in API from the start in new projects.

Basically, it includes middleware support for parsing JSON into request.POST, in addition to serializing the returned context to JSON or rendering a template/redirecting conditionally based on the request type.

查看更多
Rolldiameter
6楼-- · 2019-01-12 18:56

I ended up going with my own REST API framework for Django (that I'd love to get rid of if I can find a workable alternative), with a few custom views thrown in for corner cases I didn't want to deal with. It's worked out ok.

So a combination of 1 and 2; without some form of framework you'll end up writing the same boilerplate for the common cases.

I've also done a few stand-alone APIs. I like having them as stand-alone services, but the very fact that they stand alone from the rest of the code leads to them getting neglected. No technical reason; simply out-of-sight, out-of-mind.

What I'd really like to see is an approach that unifies Django forms and REST APIs, as they often share a lot of logic. Conceptually if your app exposes something in HTML it likely wants to expose it programmatically as well.

查看更多
闹够了就滚
7楼-- · 2019-01-12 18:56

TastyPie looks quite interesting and promising. It goes well with Django.

查看更多
登录 后发表回答