Skip to content

Commit bd325c3

Browse files
committed
Added more views examples
1 parent f45bc11 commit bd325c3

File tree

8 files changed

+117
-169
lines changed

8 files changed

+117
-169
lines changed

Pipfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ name = "pypi"
55

66
[packages]
77
django = ">=3.0.2"
8-
djangorestframework = ">=3.11.0"
9-
django-rest-swagger = "==2.2.0"
8+
djangorestframework = "==3.11.0"
109
django-opentracing = ">=1.1.0"
1110
jaeger-client = ">=4.3.0"
1211

Pipfile.lock

+1-138
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1-15
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,4 @@ Create and push the image
1616

1717
Test the image:
1818

19-
docker run -d -p 8000:8000 templatedjango
20-
21-
22-
Push to Kubernetes:
23-
24-
kubectl create -f service.yaml
25-
26-
27-
## How to contrib
28-
29-
TODO
30-
31-
### Update docs
32-
33-
sphinx-build -b html docs/ _build
19+
docker run -d -p 8000:8000 templatedjango

project/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'django.contrib.staticfiles',
4242
'django_opentracing',
4343
'rest_framework',
44-
'rest_framework_swagger',
44+
'rest_framework.authtoken',
4545
'template',
4646
]
4747

project/urls.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,28 @@
1212
Including another URLconf
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
16+
Try a private URL
17+
curl -X POST http://127.0.0.1:8000/api-token-auth/ -d '{"username": "admin", "password": "test1234"}' -H "Content-Type: application/json"
18+
curl -X GET http://127.0.0.1:8000/example-private/ -H 'Authorization: Token 3f8c6c1ed1eec35a59785f6d1963bf69ac633119'
1519
"""
1620
from django.contrib import admin
1721
from django.urls import path, include
1822
from rest_framework import routers
19-
from rest_framework_swagger.views import get_swagger_view
23+
from rest_framework.authtoken import views
2024

21-
from template.views import ColorViewSet
25+
from template.views import ColorViewSet, ColorList, ColorDetail, ExampleView
2226

2327
# Routers provide an easy way of automatically determining the URL conf.
2428
router = routers.DefaultRouter()
2529
router.register('template', ColorViewSet)
2630

27-
schema_view = get_swagger_view(title='Template API')
28-
2931
urlpatterns = [
3032
path('admin/', admin.site.urls),
31-
path('', schema_view),
33+
path('template2/', ColorList.as_view()),
34+
path('template2/<str:pk>/', ColorDetail.as_view()),
35+
path('example-private/', ExampleView.as_view()),
3236
path('', include(router.urls)),
37+
path('api-token-auth/', views.obtain_auth_token),
3338
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
3439
]

template/migrations/0001_initial.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# Generated by Django 3.0.2 on 2020-01-17 19:44
1+
# Generated by Django 3.0.2 on 2020-01-19 12:12
22

3-
import datetime
43
from django.db import migrations, models
54
import uuid
65

@@ -18,9 +17,9 @@ class Migration(migrations.Migration):
1817
fields=[
1918
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
2019
('idpublic', models.UUIDField(default=uuid.uuid4, editable=False, verbose_name='Número de referencia')),
20+
('timestamp', models.DateTimeField(auto_now_add=True)),
2121
('name', models.CharField(max_length=30)),
2222
('last_name', models.CharField(max_length=30)),
23-
('timestamp', models.DateTimeField(default=datetime.datetime.now)),
2423
],
2524
options={
2625
'db_table': 'colors',

template/serializers.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,34 @@
33
from template.models import Color
44

55

6-
# Serializers define the API representation.
7-
class ColorSerializer(serializers.HyperlinkedModelSerializer):
6+
class ColorModelSerializer(serializers.ModelSerializer):
7+
"""
8+
Example with a ModelSerializer
9+
"""
10+
811
class Meta:
912
model = Color
10-
fields = ('id', 'name', 'timestamp')
13+
fields = ('idpublic', 'name', 'timestamp')
14+
15+
16+
class ColorSerializer(serializers.Serializer):
17+
"""
18+
Example with a Serializer
19+
"""
20+
idpublic = serializers.CharField(read_only=True)
21+
name = serializers.CharField(required=True, allow_blank=False, max_length=100)
22+
timestamp = serializers.DateTimeField(read_only=True)
23+
24+
def create(self, validated_data):
25+
"""
26+
Create and return a new `Color` instance, given the validated data.
27+
"""
28+
return Color.objects.create(**validated_data)
29+
30+
def update(self, instance, validated_data):
31+
"""
32+
Update and return an existing `Color` instance, given the validated data.
33+
"""
34+
instance.name = validated_data.get('name', instance.name)
35+
instance.save()
36+
return instance

0 commit comments

Comments
 (0)