Skip to main content

Expose Django's admin as a RESTFUL service

  • Support all of restful api
  • Auto generat serializers
  • Use Django Rest Framework
  • Fully customization support
  • Useing Django Rest Framework ViewSet as AdminModels
  • Support default Django admin auth permission
  • Support Django custom model permissin with simple configuration
  • Useing Django Rest Framework Serializer(or ModelSerializer) as request validators
  • Support Single Serializer calss to customize your detail view
  • Auto generate documentation for CURDs
  • Pagination and abillity to change paginator
  • Support custom action with premission
  • Support all features in DRF

Todo

  • Add some documentation
  • Add custom route Api documentation
  • Add list_display
  • Add Filter And Search
  • Add exlude
  • Add Fields
  • Example inline
  • Localization
  • Somthings thats need in future

How To Install

pip install django-restful-admin
add to INSTALED_APPS

INSTALLED_APPS = [
    ...  
   'rest_framework',   
  'django_restful_admin',    
   ...  
]  

How To use

you need only add the bellow code to admin.py in your apps

from django_restful_admin import admin
from yourapp.models improt FisrtModel, ScoundModel   

admin.site.register(FisrtModel)    
admin.site.register(ScoundModel)   

Then add URL to your project urls.py

from django_restful_admin import admin as api_admin 
  urlpatterns = [    
      ...   
      path('apiadmin/', api_admin.site.urls),  
  	 ...     
]  

Run the project and open URL http://your-ip:port/apiadmin/

enjoy!

Change Log

  • export admin in init.py
  • Add default django auth permissions support
  • Add admin.register decorator

Customization

Example

Create a new Django project

$ django-admin startproject example`
$ cd example
$ python manage.py startapp blog

Create blog app models in blog/models.py

from django.db import models

class Category(models.Model)
	title = models.CharField(  
	  max_length=255  
	)

class Post(models.Model):
	title = models.CharField(  
	  max_length=255  
	)
	summery = models.TextField()
	description = models.TextField()
	category = models.ForeignKey(  
		Category,  
		related_name='products',  
		on_delete=models.CASCADE  
	)

Add your blog app in INSTALLED_APPS in example/settings.py

INSTALLED_APPS = [
		# Django default apps...
		'rest_framework',
		'django_restful_admin',

		'blog'
]

Add admin URLs to django URLs in example/urls.py

from django.conf.urls import url  
from django.contrib import admin  
from django_restful_admin import admin as api_admin  
from django.urls import path  


urlpatterns = [  
  path('apiadmin/', api_admin.site.urls),  # this line added
  # path('admin/', admin.site.urls),
  # your apis custom must be set here  
]

Register your model to restful admin site in blog/admin.py

from django_restful_admin import admin as api_admin  
from blog.models import *    

api_admin.site.register(Post) 
api_admin.site.register(Category) 

Add View and use decorators blog/admin.py

...
@api_admin.register(Category, Product)  
class MyCustomApiAdmin(BaseRestFulModelAdmin):  
    authentication_classes = (CustomTokenAuthentication,)
	permission_classes = [IsAuthenticated] 
... 

Read more about authentication and permission DRF Documentation.

Customize serialization

At first, you must define your serializer class Make serializers.py file in blog Open blog/serializers.py and make serializer like this:

from rest_framework import serializers
from .models import *
class ProductSerializer(serializers.ModelSerializer):
	class Meta:
		model = Product
		feilds = ('id', 'title')
class CategorySerializer(serializers.ModelSerializer):
	class Meta:
		model = Category
		fields = ('id','title)

class SingleProductSerializer(serializers.ModelSerializer):
	category = CategorySerializer(read_only=True)
	class Meta:
		model = Product
		feilds = ('id', 'title', 'summery', 'description', 'category')
from .serializers import ProductSerializer, SingleProductSerializer
@api_admin.register(Product)  
class ProductApiAdmin(BaseRestFulModelAdmin):  
    serializer_class = ProductSerializer
    single_serializer_class = SingleProductSerializer

serializer_class use for serialize list of objects but single_serializer_class use for serializer view signle object, update, partial update and create.

Add a custom route with permission

@api_admin.register(Product)  
class MyCustomApiAdmin(BaseRestFulModelAdmin):

	@api_admin.action(permission='product.view_product', detail=True, methods=['GET'], url_path=r'my-custom-action/(?P<another_key>[^/.]+)')  
	def my_custom_action(self, request, pk, another_key):
		pass
		## Do what you want to do
		## this action make url like this /apiadmin/blog/product/2/my-custom-action/XXX

If you want to except permission you just send permission=True, for creating custom permission you can pass closure function or lambda to permission like this permission=lambda: view, action, request, obj=None: True

Contribute

if you think you can help me please let's start.

Metadata

Release files for django-restful-admin 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-restful-admin 1.1.1
File Size Uploaded Provenance
django-restful-admin-1.1.1.tar.gz 7.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-restful-admin 1.1.1
File Interpreter ABI Platform
django_restful_admin-1.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.2 kB

Release files / django-restful-admin-1.1.1.tar.gz

Download URL django-restful-admin-1.1.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a29eae4aac1073513c761f462ca264b169f29fc6e5fd0c4bd50fbbe8609611cb
BLAKE2b-256 checksum
How to use checksums
fe63ca13133c23260968cc9e6167ef5d71ff4c5fdf790046087a72040fb0ab3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

Release files / django_restful_admin-1.1.1-py2.py3-none-any.whl

Download URL django_restful_admin-1.1.1-py2.py3-none-any.whl
Size 10.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7e73f6779ce1835c96436ac63500ab3c28fb9968845d9446155c14ae0a1642c0
BLAKE2b-256 checksum
How to use checksums
1e103b2bcc129e84e525161e81714371776c2a869e5be1903b37f94e76ab7677
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

Security for django-restful-admin 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page