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

barseghyanartur avatar bmihelac avatar dependabot[bot] avatar e-stepanov avatar rafaellehmkuhl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

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

sorting doesn't work

When click to sort, arrow appears. but nothing else happens.

Anybody has same issue?

File Inout

Hi there
I want to use fileinput but I get this error :
The submitted data was not a file. Check the encoding type on the form
when I use the back end ( using rest framework ) no problem but when I want to upload using react-admin I get the error.

TS2305: Module '"ra-core"' has no exported member 'Pagination'.

I am getting this error all of a sudden

ERROR in src/ra-data-django-rest-framework/src/index.ts:4:3
TS2305: Module '"ra-core"' has no exported member 'Pagination'.
    2 | import {
    3 |   Identifier,
  > 4 |   Pagination,
      |   ^^^^^^^^^^
    5 |   Sort,
    6 |   Filter,
    7 |   fetchUtils,

ERROR in src/ra-data-django-rest-framework/src/index.ts:5:3
TS2305: Module '"ra-core"' has no exported member 'Sort'.
    3 |   Identifier,
    4 |   Pagination,
  > 5 |   Sort,
      |   ^^^^
    6 |   Filter,
    7 |   fetchUtils,
    8 |   DataProvider,

ERROR in src/ra-data-django-rest-framework/src/index.ts:6:3
TS2305: Module '"ra-core"' has no exported member 'Filter'.
    4 |   Pagination,
    5 |   Sort,
  > 6 |   Filter,
      |   ^^^^^^
    7 |   fetchUtils,
    8 |   DataProvider,
    9 | } from 'ra-core';

ERROR in src/RaDataDRFDataProvider.ts:4:3
TS2305: Module '"ra-core"' has no exported member 'Pagination'.
    2 | import {
    3 |   Identifier,
  > 4 |   Pagination,
      |   ^^^^^^^^^^
    5 |   Sort,
    6 |   Filter,
    7 |   fetchUtils,

ERROR in src/RaDataDRFDataProvider.ts:5:3
TS2305: Module '"ra-core"' has no exported member 'Sort'.
    3 |   Identifier,
    4 |   Pagination,
  > 5 |   Sort,
      |   ^^^^
    6 |   Filter,
    7 |   fetchUtils,
    8 |   DataProvider,

ERROR in src/RaDataDRFDataProvider.ts:6:3
TS2305: Module '"ra-core"' has no exported member 'Filter'.
    4 |   Pagination,
    5 |   Sort,
  > 6 |   Filter,
      |   ^^^^^^
    7 |   fetchUtils,
    8 |   DataProvider,
    9 | } from 'ra-core';


Whats the fix ?

Cannot read properties of undefined (reading 'id') after deleteMany

Handling of the server response in the deleteMany method expects json with id field but django rest framework returns response without body.

According to react-admin docs the ids of the deleted records are optional in deleteMany. So that an empty array can be returned as a data value.

I can create a pull request if you confirm this bug.

Are there permissions available?

On the example client (example/client/src/index.js) you use permissions for displaying or not the users section:

{permissions => [
    <Resource name="posts" {...posts} />,
    <Resource name="comments" {...comments} />,
    permissions ? <Resource name="users" {...users} /> : null,
    <Resource name="tags" {...tags} />,
]}

Are those permissions available? Because as far as I could see on the tokenAuthProvider.ts, only the authentication token is stored locally.

data provider error

My code is like this:
`
import drfProvider from "ra-data-django-rest-framework";

const dataProvider = drfProvider("http://localhost:8000");
`

But, I get this error:

The response to 'getList' must be like { data : [{ id: 123, ...}, ...] }, but the received data items do not have an 'id' key. The dataProvider is probably wrong for 'getList' console.<computed> @ 0.chunk.js:167134

What am I doing wrong?

EDIT: need to include id in serializer fields:

class SaleSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Sale fields = ['id', 'date', ...]

Authorization

Dear all,
could you please explain how to use Authorization? for example, how to set User Permissions and use it like this:
{permissions === 'admin' && <TextInput source="role" validate={[required()]} />}
bests,

Upgrade to react-admin v4

Hi and thanks for this lib.

Can you please upgrade the react-admin version that this lib is using to v4?

Disable pagination

I disabled pagination in List component but still needs count and data field in response

The response to 'getList' must be like { data : [...] }, but the received data is not an array. The dataProvider is probably wrong for 'getList'*

    <List {...props} pagination={false}>
    ...
     </List>

there is a way to ignore pagination in response?
Great work BTW

Failed on loading initial data

Running ./migrate.py loaddata initial returns the following error:

./manage.py loaddata initial
Traceback (most recent call last):
  File "/home/rafaellehmkuhl/Development/dash-test/ra-data-django-rest-framework/example/backend/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/home/rafaellehmkuhl/Development/dash-test/ra-data-django-rest-framework/example/backend/.venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)

  [...]

  File "/home/rafaellehmkuhl/Development/dash-test/ra-data-django-rest-framework/example/backend/.venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: Problem installing fixture '/home/rafaellehmkuhl/Development/dash-test/ra-data-django-rest-framework/example/backend/exampleapp/fixtures/initial.json': Could not load contenttypes.ContentType(pk=7): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

Create, Update data provider use get instead of post or patch

Hello. Thank you very much for such a project. I was testing and in the example, when I create a new post, the data provider sends a get request to the backend. Is this problem of data provider? You can see this problem when you create new post in example. Thank you
image

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.