https://github.com/bmihelac/ra-data-django-rest-framework
react-admin data provider for Django REST framework
https://github.com/bmihelac/ra-data-django-rest-framework
django djdjango-rest-framework python react react-admin
Last synced: 8 months ago
JSON representation
react-admin data provider for Django REST framework
- Host: GitHub
- URL: https://github.com/bmihelac/ra-data-django-rest-framework
- Owner: bmihelac
- License: mit
- Created: 2020-05-25T19:52:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-26T21:16:57.000Z (over 3 years ago)
- Last Synced: 2025-04-10T01:08:32.338Z (about 1 year ago)
- Topics: django, djdjango-rest-framework, python, react, react-admin
- Language: JavaScript
- Homepage:
- Size: 3.78 MB
- Stars: 106
- Watchers: 6
- Forks: 31
- Open Issues: 45
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
# ra-data-django-rest-framework
[react-admin](https://marmelab.com/react-admin/) data and authentication provider for [Django REST
framework](https://www.django-rest-framework.org/).
[](https://npm.im/ra-data-django-rest-framework)
[](./LICENSE)

[](https://codecov.io/gh/bmihelac/ra-data-django-rest-framework)
ra-data-django-rest-framework includes backend and client example application
and tests.
## Install
```bash
npm install ra-data-django-rest-framework
```
## Usage
```javascript
import drfProvider from 'ra-data-django-rest-framework';
const dataProvider = drfProvider("/api");
```
## Features
* Sorting
* Pagination
* Filtering
* Authentication
### Sorting
Ordering for
[OrderingFilter](https://www.django-rest-framework.org/api-guide/filtering/#orderingfilter)
is supported.
### Pagination
Currently pagination with
[PageNumberPagination](https://www.django-rest-framework.org/api-guide/pagination/#pagenumberpagination)
is supported.
Default `PageNumberPagination` has `page_size_query_param` set to `None`,
overide to be able to set *Rows per page*, ie:
```python
from rest_framework.pagination import PageNumberPagination
class PageNumberWithPageSizePagination(PageNumberPagination):
page_size_query_param = 'page_size'
```
### Filtering
ra-data-django-rest-framework supports:
* [Generic Filtering](https://www.django-rest-framework.org/api-guide/filtering/#generic-filtering)
* [DjangoFilterBackend](https://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend)
### Authentication
#### tokenAuthProvider
`tokenAuthProvider` uses
[TokenAuthentication](https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication)
to obtain token from django-rest-framework. User token is saved in `localStorage`.
`tokenAuthProvider` accepts options as second argument with
`obtainAuthTokenUrl` key. Default URL for obtaining a token is `/api-token-auth/`.
`fetchJsonWithAuthToken` overrides *httpClient* and adds authorization header
with previously saved user token to every request.
```javascrtipt
import drfProvider, { tokenAuthProvider, fetchJsonWithAuthToken } from 'ra-data-django-rest-framework';
const authProvider = tokenAuthProvider()
const dataProvider = drfProvider("/api", fetchJsonWithAuthToken);
```
#### jwtTokenAuthProvider
`jwtTokenAuthProvider` uses
[JSON Web Token Authentication](https://www.django-rest-framework.org/api-guide/authentication/#json-web-token-authentication)
to obtain token from django-rest-framework. User token is saved in `localStorage`.
`jwtTokenAuthProvider` accepts options as second argument with
`obtainAuthJWTTokenUrl` key. Default URL for obtaining a token is `/api/token/`.
`fetchJsonWithAuthJWTToken` overrides *httpClient* and adds authorization header
with previously saved user token to every request.
```javascrtipt
import drfProvider, { jwtTokenAuthProvider, fetchJsonWithAuthJWTToken } from 'ra-data-django-rest-framework';
const authProvider = jwtTokenAuthProvider()
const dataProvider = drfProvider("/api", fetchJsonWithAuthJWTToken);
```
## Example app
### Django application with django-rest-framework
Setup virtual envirnoment, install requirements and load initial data:
```bash
cd example/backend
virtualenv .venv
source .venv/bin/activate
pip install -r requirements.txt
./manage.py migrate
./manage.py loaddata initial
```
Run server:
```bash
./manage.py runserver
```
Admin credentials in the example app are:
`admin`
`password`
### React-admin demo application
```bash
yarn install # install ra-data-django-rest-framework
cd example/client
yarn install
yarn start
```
You can now view example app in the browser: http://localhost:3000
Login with user `admin`, password is `password` or create new users in Django
admin dashboard or shell.
By default the ``rest_framework.authentication.TokenAuthentication`` will be
used. To use ``rest_framework_simplejwt.authentication.JWTAuthentication``, set
the value of the ``REACT_APP_USE_JWT_AUTH`` variable in the .env
file (example/client/.env) to true, as shown below:
```text
REACT_APP_USE_JWT_AUTH=true
```
## Contributing
This project was bootstrapped with [TSDX](https://github.com/jaredpalmer/tsdx).
All features that TSDX provides should work here too.
```bash
yarn start
```
```bash
yarn test
```
## TODO
* examples for image upload