Skip to content

Commit 3f5f167

Browse files
Add urls, views, serializers: for User WIP
1 parent 3b9b270 commit 3f5f167

File tree

6 files changed

+55
-20
lines changed

6 files changed

+55
-20
lines changed

accounts/serializers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from rest_framework_json_api import serializers, relations
2+
from accounts.models import User
3+
4+
5+
class UserSerializer(serializers.ModelSerializer):
6+
class Meta:
7+
model = User
8+
fields = ('username',)

accounts/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.conf.urls import url, include
2+
from accounts import views
3+
from rest_framework import routers
4+
5+
6+
router = routers.DefaultRouter(trailing_slash=False)
7+
router.register(r'users', views.UserViewSet, base_name='User')
8+
9+
urlpatterns = [
10+
url(r'^', include(router.urls)),
11+
]

accounts/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
from django.shortcuts import render
1+
from rest_framework import viewsets
2+
from accounts.models import User
3+
from accounts.serializers import UserSerializer
24

3-
# Create your views here.
5+
class UserViewSet(viewsets.ModelViewSet):
6+
queryset = User.objects.all()
7+
serializer_class = UserSerializer

api/settings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@
104104
},
105105
]
106106

107+
REST_FRAMEWORK = {
108+
'PAGE_SIZE': 100,
109+
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
110+
'DEFAULT_PARSER_CLASSES': (
111+
'rest_framework_json_api.parsers.JSONParser',
112+
'rest_framework.parsers.FormParser',
113+
'rest_framework.parsers.MultiPartParser',
114+
'rest_framework.parsers.JSONParser',
115+
),
116+
'DEFAULT_RENDERER_CLASSES': (
117+
'rest_framework_json_api.renderers.JSONRenderer',
118+
'rest_framework.renderers.BrowsableAPIRenderer',
119+
),
120+
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
121+
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
122+
}
123+
124+
JSON_API_FORMAT_KEYS = 'underscore'
125+
JSON_API_FORMAT_TYPES = 'underscore'
126+
JSON_API_PLURALIZE_TYPES = True
127+
JSON_API_PLURALIZE_RELATION_TYPE = True
128+
JSON_API_FORMAT_RELATION_KEYS = True
107129

108130
# Internationalization
109131
# https://docs.djangoproject.com/en/2.1/topics/i18n/

api/urls.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
"""api URL Configuration
2-
3-
The `urlpatterns` list routes URLs to views. For more information please see:
4-
https://docs.djangoproject.com/en/2.1/topics/http/urls/
5-
Examples:
6-
Function views
7-
1. Add an import: from my_app import views
8-
2. Add a URL to urlpatterns: path('', views.home, name='home')
9-
Class-based views
10-
1. Add an import: from other_app.views import Home
11-
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12-
Including another URLconf
13-
1. Import the include() function: from django.urls import include, path
14-
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15-
"""
1+
from django.conf.urls import url, include
162
from django.contrib import admin
17-
from django.urls import path
3+
4+
admin_root_url = r'^services/admin/'
185

196
urlpatterns = [
20-
path('admin/', admin.site.urls),
7+
url(admin_root_url, admin.site.urls),
8+
url(r'^services/api/', include('accounts.urls')),
219
]

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Django==2.1.2
2-
psycopg2-binary==2.7.5
2+
psycopg2-binary==2.7.5
3+
djangorestframework==3.8.2
4+
djangorestframework-jsonapi==2.2.0

0 commit comments

Comments
 (0)