Giter VIP home page Giter VIP logo

ra-data-django-rest-framework's Introduction

ra-data-django-rest-framework

react-admin data and authentication provider for Django REST framework.

Stable Release license CI codecov

ra-data-django-rest-framework includes backend and client example application and tests.

ra-data-django-rest-framework

Install

npm install ra-data-django-rest-framework

Usage

import drfProvider from 'ra-data-django-rest-framework';
const dataProvider = drfProvider("/api");

Features

  • Sorting
  • Pagination
  • Filtering
  • Authentication

Sorting

Ordering for OrderingFilter is supported.

Pagination

Currently pagination with PageNumberPagination is supported.

Default PageNumberPagination has page_size_query_param set to None, overide to be able to set Rows per page, ie:

from rest_framework.pagination import PageNumberPagination


class PageNumberWithPageSizePagination(PageNumberPagination):
    page_size_query_param = 'page_size'

Filtering

ra-data-django-rest-framework supports:

Authentication

tokenAuthProvider

tokenAuthProvider uses 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.

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 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.

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:

cd example/backend
virtualenv .venv
source .venv/bin/activate
pip install -r requirements.txt
./manage.py migrate
./manage.py loaddata initial

Run server:

./manage.py runserver

Admin credentials in the example app are:

admin password

React-admin demo application

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:

REACT_APP_USE_JWT_AUTH=true

Contributing

This project was bootstrapped with TSDX. All features that TSDX provides should work here too.

yarn start
yarn test

TODO

  • examples for image upload

ra-data-django-rest-framework's People

Contributors

goldendev176743 avatar

Stargazers

LiveDeveloper823 avatar  avatar  avatar sanjaya avatar lily avatar

Watchers

lily avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.