https://github.com/miserlou/django-easy-api
Give your website an API by changing a single line of code!
https://github.com/miserlou/django-easy-api
Last synced: 7 months ago
JSON representation
Give your website an API by changing a single line of code!
- Host: GitHub
- URL: https://github.com/miserlou/django-easy-api
- Owner: Miserlou
- License: mit
- Created: 2014-07-28T22:40:11.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2021-03-25T21:45:38.000Z (over 4 years ago)
- Last Synced: 2025-03-17T04:21:17.637Z (7 months ago)
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

django-easy-api
===============**django_easy_api** lets you create an API for your Django project by changing only a single line of code!
The easiest way to use **django_easy_api** is to simply replace render_to_response:
```python
# from django.shortcuts import render_to_response
from easy_api.shortcuts import render_to_response
```So if this returns a rendered HTML view:
http://yoursite.com/item/23
then this returns a JSON serialized view of the same page:
http://yoursite.com/item/23?api=json
and this returns an XML serialized view of the same page:
http://yoursite.com/item/23?api=xml
Hooray! You literally don't have to do anything else.
#### Installation
0. Install django-easy-split
```python
pip install django-easy-api
```1. Add 'easy_api' to your INSTALLED_APPS setting like this:
```python
INSTALLED_APPS = (
...
'easy_api',
)
```## Usage
The easiest way to use **django_easy_api** is to simply replace render_to_response:
```python
# from django.shortcuts import render_to_response
from easy_api.shortcuts import render_to_response
```Now, all of your views will have API versions as well!
If you only want to use **django_easy_api** for certain views, you can use it explicitly:
```python
from easy_api.shortcuts import render_to_easy_api_response
```
and then use it in your view instead of render_to_response.## Advanced Usage
If you want to control, which fields of your model are exposed in your feeds, you can define a function for you model called easy_api_fields, which should return a list of the name of the properties you want to expose.
```python
def easy_api_fields(self):
return['name', 'number']
```by default, it uses the keys in the object's __to_dict()__ method. For computed properties, you can use python's __property__ function.
## Notes
Okay, okay, so it's not a FULL API. It's just for GETs. If you want a full-featured API, look at [Django-Rest-Framework](http://www.django-rest-framework.org/). However, if you just need a machine-readable version of your site, this is far and away the easiest way to do it!
### TODO:
* I don't know what it does for ForeignKeys. To be tested!
* I haven't tested this in production, but it should work.