Skip to main content

Python Django RestFul Admin (forked by PPB)

Project description

This is a fork of django-restful-admin, all credits to @amirasaran.

Expose Django's admin as a RESTFUL service

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

Todo

  • Add some documentation
  • Add custom route Api documentation
  • Add list_display
  • Add Filter And Search
  • Add exlude
  • Add Fields
  • Example inline
  • Localization
  • Somethings that's 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.

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

django-restful-admin-ppb-1.1.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

django_restful_admin_ppb-1.1.4-py2.py3-none-any.whl (8.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-restful-admin-ppb-1.1.4.tar.gz.

File metadata

  • Download URL: django-restful-admin-ppb-1.1.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for django-restful-admin-ppb-1.1.4.tar.gz
Algorithm Hash digest
SHA256 2cbb031087d114abeae84dba8bb27fae53fa5416d5df907f518e9e8506f705bb
MD5 ca9bafd843527dd724b945408d5c151b
BLAKE2b-256 af42ca4a400fc7a4f301789adf11389d0e5adcb3ab008d65015541dbafa203dc

See more details on using hashes here.

File details

Details for the file django_restful_admin_ppb-1.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: django_restful_admin_ppb-1.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for django_restful_admin_ppb-1.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 264061e60ddde7a5dc215d06eceb08302f6f1d31b5c1da58633dc149eeb787e9
MD5 75778f20f3ad8e72e5eacec836fe1f99
BLAKE2b-256 aca84afe9256320bd182a45af7d0c74e7aac53c45876561e93f272fb5efe6047

See more details on using hashes here.

Supported by

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