Generic RESTful API Server Use Django. Define models, and your APIs are already to use.
Features:
- API
- Admin
- Reversion
- Tags
- Commit messages.
git clone https://github.com/sadnoodles/gras.git
pip install .
- Create an Django project.
django-admin startproject test_project
- Create an app.
cd test_project
python manage.py startapp blog
- Create Models
open test_project\blog\models.py
add two models.
from django.db import models
from gras.models import CommonAttr
# Create your models here.
class Blog(CommonAttr):
title = models.CharField(max_length=200, blank=True)
content = models.TextField()
class Comment(CommonAttr):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
content = models.TextField()
- Add admin
open test_project\blog\admin.py
add two models.
from django.contrib import admin
from gras.shortcut import init_admins
# Register your models here.
init_admins('blog')
- Add API urls.
open test_project\blog\urls.py
add urls.
from django.conf.urls import url, include
from gras.shortcut import init_urls
urlpatterns = init_urls('blog')
And add theses urls to main url file.
open test_project\test_project\urls.py
.
urlpatterns = [
...
url(r'^blog/', include(('blog.urls', 'blog'), namespace='blog')),
...
]
- Add Settings.
File: test_project\test_project\settings.py
INSTALLED_APPS = [
...
'django_filters',
'rest_framework',
'gras',
'blog',
...
]
...
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
'rest_framework.filters.SearchFilter',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
'DEFAULT_PAGINATION_CLASS': (
'gras.pagination.PageNumberPagination'
),
'PAGE_SIZE': 18,
'SEARCH_PARAM':'s',
'DATETIME_FORMAT': DATETIME_FORMAT,
}
- Run.
Migrate db
python manage.py makemigrations
python manage.py migrate
Create super user
python manage.py createsuperuser
Run
python manage.py runserver
Admin page
REST API