Skip to content

Commit f927b0d

Browse files
committed
Part 3
1 parent b284276 commit f927b0d

5 files changed

Lines changed: 35 additions & 19 deletions

File tree

db.sqlite3

0 Bytes
Binary file not shown.

medium/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'django.contrib.sessions',
3939
'django.contrib.messages',
4040
'django.contrib.staticfiles',
41+
'rest_framework',
4142
'reviews'
4243
]
4344

medium/urls.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
"""medium URL Configuration
2-
3-
The `urlpatterns` list routes URLs to views. For more information please see:
4-
https://docs.djangoproject.com/en/3.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-
"""
161
from django.contrib import admin
17-
from django.urls import path
2+
from django.conf.urls import url, include
3+
from reviews.views import ProductViewSet
4+
from rest_framework.routers import DefaultRouter
5+
6+
router = DefaultRouter()
7+
router.register(r'product', ProductViewSet, basename='Product')
188

199
urlpatterns = [
20-
path('admin/', admin.site.urls),
10+
url(r'^admin/', admin.site.urls),
11+
url(r'^', include(router.urls)),
2112
]
13+

reviews/serializers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from rest_flex_fields import FlexFieldsModelSerializer
2+
from .models import Product, Category
3+
4+
5+
class CategorySerializer(FlexFieldsModelSerializer):
6+
class Meta:
7+
model = Category
8+
fields = ['pk', 'name']
9+
10+
11+
class ProductSerializer(FlexFieldsModelSerializer):
12+
class Meta:
13+
model = Product
14+
fields = ['pk', 'name', 'content', 'created', 'updated']
15+
expandable_fields = {
16+
'category': (CategorySerializer, {'many': True})
17+
}

reviews/views.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
from django.shortcuts import render
1+
from rest_framework.viewsets import ReadOnlyModelViewSet
2+
from .serializers import ProductSerializer
3+
from .models import Product
24

3-
# Create your views here.
5+
6+
class ProductViewSet(ReadOnlyModelViewSet):
7+
8+
serializer_class = ProductSerializer
9+
queryset = Product.objects.all()

0 commit comments

Comments
 (0)