Skip to main content

A package to generate CRUD endpoints for registered models with the Django-REST Framework.

Project description

This is a fork of BdVade/DRF-admin project

This repo has been renamed in order to avoid confusions with BdVade/DRF-admin and will be mantained by myself.

Any contribution and feature requests are welcome!

dj-rest-admin

A package to generate CRUD endpoints for registered models with the Django-REST Framework.

Requirements

Installation

To install run:

pip install drf-admin

Usage

  • Add rest_admin.py in your app dirs for defining RestModelAdmin
  • Import dj_rest_admin in the rest_admin.py
  • Call dj_rest_admin.site.register(Model) Model being the model to register
  • Add rest admin to your urls.py file.
  • [Optional] Add dj_rest_admin to your INSTALLED_APPS for admin autodiscover.

Prerequisite

  • rest_framework should be properly set up to use this package hitch free

A sample of it's configuration in the settings file:

 REST_FRAMEWORK={
            'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
            'TEST_REQUEST_RENDERER_CLASSES': [
                'rest_framework.renderers.MultiPartRenderer',
                'rest_framework.renderers.JSONRenderer',
                'rest_framework.renderers.TemplateHTMLRenderer', ],
            "DEFAULT_AUTHENTICATION_CLASSES": [
                'rest_framework.authentication.SessionAuthentication',
                'rest_framework.authentication.BasicAuthentication'
            ],
            "DEFAULT_PERMISSION_CLASSES": [
                'rest_framework.permissions.AllowAny',
            ]
        }

For example:

models.py

from django.db import models

class TestModel(models.Model):
    age = models.IntegerField()

admin.py

from .models import TestModel
import dj_rest_admin

dj_rest_admin.site.register(TestModel)

urls.py

from restadmin import site
from django.urls import path




urlpatterns = [
    ...
    path('restadmin/', site.urls),
    ...
]

Customization

This package allows you to specify the following when registering your model

  • serializer_or_modeladmin: A Model Serializer Class or a subclass of RestModelAdmin
  • permission_classes: A list of Permission classes
  • pagination_classs: A Pagination Class

An example of how a call to the register method with all 3 would look is :

dj_rest_admin.site.register(TestModel, serializer_or_modeladmin=AdminSerializer, permission_classes=[ReadOnly], 
                        pagination_class=LargeResultsSetPagination)

RestModelAdmin expose the same interface as ModelViewSet so you can add the whole customizations that ModelViewSet offers. This includes:

  • Custom querysets
  • redifining defaults methods
  • add actions as ModelViewSet's exta actions

You can also register models with the register decorator

Example:

from dj_rest_admin import register, RestModelAdmin
from .models import TestModel

@register(TestModel)
class TestRestModelAdmin(RestModelAdmin):

    serializer_class = MyCustomSerializer # Optional. A default is provided if None defined

    def get_queryset(self):
        queryset = TestModel.objects.filter(age__lt=30)
        return queryset

Endpoint Documentation

  • This requires you to have coreapi installed

A page to document the Endpoints generated can be accessed by adding the following to your base urls file

from dj_rest_admin import site


urlpatterns = [
   ...
    path('restadmin-docs/', site.docs)
    ...
]

Using this would require you to have your default schema Class set in your REST_FRAMEWORK config in your settings.py file E.g

REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' }

Run your server and you can find the documentation at http://127.0.0.1:8000/restadmin-docs NOTE: The Documentation page is restricted to staff only(is_staff has to be True)

Tests

To run the tests:

From the base directory run :

python load_tests.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-rest-admin-1.0.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_rest_admin-1.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file dj-rest-admin-1.0.2.tar.gz.

File metadata

  • Download URL: dj-rest-admin-1.0.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for dj-rest-admin-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f1ed56ae0186fc75a9c4d1b8e303ced4c75476db965cd0711eecefb3ed703f0f
MD5 ef3603bad5af398510091eace44cb7b6
BLAKE2b-256 cc6e1a83ad0bdae7cdae1a92b8bde41823aae032c251e2cf38f845910bd896f1

See more details on using hashes here.

File details

Details for the file dj_rest_admin-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: dj_rest_admin-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for dj_rest_admin-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a4cf1877727a32781878f9672c34559f1a2633e8bf2eab7ff4ff0b7140be633
MD5 78b872cfc378ec6c03f5cc3b306fac83
BLAKE2b-256 cd5db6dfe5a258bd03d1f1ff6fd335799d470e48dd2a2b671cee42a9acda3f69

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page