Autogenerate API's for models. Expose core Django functionality. Validate against existing forms.
Project description
DRFE generates RESTful API’s for any Django models. It builds on Django Rest Framework.
Prerequisite
pip install djangorestframework
Add rest_framework to your INSTALLED_APPS setting in settings.py.
Installation
Install or add djangorestframework-extras to your Python path.
Add rest_framework_extras to your INSTALLED_APPS setting in settings.py.
Feature List
Generating default serializers and viewsets for all known applications to create RESTful API’s.
Registering all viewsets known to the application with the Django Rest Framework router.
Custom serializers and permissions for the default user, the staff users and superusers.
Custom FormMixin that Delegates validation to a normal Django form.
Custom Hyperlink fields and serializer, HyperlinkedRelatedField and HyperlinkedModelSerializer
Usage
User permissions and the custom UsersViewSet
djangorestframework-extras provides a custom ViewSet UsersViewSet with serializers and permissions for the default user, the staff user and the superuser.
Register UsersViewSet through the DefaultRouter:
from rest_framework_extras.users.viewsets import UsersViewSet router = routers.DefaultRouter() router.register(r'users', UsersViewSet, 'user')
Discovery and registration of ViewSets
Enable discovery and registration of default serializers and viewsets by adding the following to urls.py:
from rest_framework import routers import rest_framework_extras router = routers.DefaultRouter() rest_framework_extras.discover(router) rest_framework_extras.register(router) urlpatterns = [ url(r"^api/(?P<version>(v1))/", include(router.urls)) ]
Going through the code line by line:
Line 1 & 3: The router and DefaultRouter classes connects the views and urls automatically and also creates the API root.
Line 5: The new discover function generates default serializers and viewsets. This function should be run before normal registration.
Line 6: The new register function registers all viewsets (including the UsersViewSet), overriding any items already registered with the same name.
Line 9: Define the urls by including router.urls.
Available Settings
REST_FRAMEWORK_EXTRAS
blacklist: A dictionary of the models to blacklist. By default the following models are blacklisted:
"REST_FRAMEWORK_EXTRAS": { "blacklist": { "sessions-session": {}, "admin-logentry": {} }, "authentication-classes": (SessionAuthentication, BasicAuthentication), "permission-classes": (DjangoModelPermissions,) }
Tips
Change the name of the registered user model by changing the mapping parameter, such as:
rest_framework_extras.register(router, mapping=(("user", UsersViewSet),))
Restrict models that will be displayed through the Django Rest Framework by using the only and override parameters. Define a comma separated list, such as:
rest_framework_extras.discover(router, only=["auth-user", "auth-permission"])
todo: document override
Unit Testing
Run tests by using the following command:
python manage.py test rest_framework_extras.tests --settings=rest_framework_extras.tests.settings.19
License
Please see the License requirements in the LICENSE file of this repository.
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
Built Distribution
File details
Details for the file djangorestframework-extras-0.2.tar.gz
.
File metadata
- Download URL: djangorestframework-extras-0.2.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebbb214831dda453dd341075f6a3e30bc36cd70c9d27a6130d4563855d8cd288 |
|
MD5 | 82a1d15500dda1175d08e3b9f297aaae |
|
BLAKE2b-256 | f81efdb8c51d4cfc351ef4d4a8285bafc03bbd47eae61e02e4c4a9a3af0d7dfd |
File details
Details for the file djangorestframework_extras-0.2-py2.7.egg
.
File metadata
- Download URL: djangorestframework_extras-0.2-py2.7.egg
- Upload date:
- Size: 34.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 802d925a1a18b1742763491dd2701f0f759350d2cf2ca804b7a6527851024e73 |
|
MD5 | 4db8f960647bc9e1672422633d9e6e1b |
|
BLAKE2b-256 | b57f2e2c07690fb7dc5392667fa4eb42a77b03f6689e22d0d7e910b4154ac48e |