rest framwork 小試身手

2022-09-15 09:51:09 字數 4034 閱讀 6874

models.py

from django.db import models

class course(models.model):

"""課程表

"""title = models.charfield(verbose_name='課程名稱',max_length=32)

course_img = models.charfield(verbose_name='課程',max_length=64)

level_choices = (

(1,'初級'),

(2,'中級'),

(3,'高階'),

)level = models.integerfield(verbose_name='課程難易程度',choices=level_choices,default=1)

def __str__(self):

return self.title

class coursedetail(models.model):

"""課程詳細

"""course = models.onetoonefield(to='course')

slogon = models.charfield(verbose_name='口號',max_length=255)

why = models.charfield(verbose_name='為什麼要學?',max_length=255)

recommend_courses = models.manytomanyfield(verbose_name='推薦課程',to='course',related_name='rc')

def __str__(self):

return "課程詳細:"+self.course.title

class chapter(models.model):

"""章節

"""num = models.integerfield(verbose_name='章節')

name = models.charfield(verbose_name='章節名稱',max_length=32)

course = models.foreignkey(verbose_name='所屬課程',to='course')

def __str__(self):

return self.name

urls.py

from django.conf.urls import url,include

from api.views import course

urlpatterns = [

url(r'^course/$', course.courseview.as_view()),

url(r'^course/(?p\d+)$', course.courseview.as_view()),

url(r'^test$', course.test),

]

course.py

from rest_framework.views import apiview

from rest_framework.response import response

from api import models

from rest_framework import serializers

class courseserializer(serializers.modelserializer):

class meta:

model = models.course

fields = "__all__"

class coursedetailserializer(serializers.modelserializer):

# one2one/fk/choice

title = serializers.charfield(source='course.title')

img = serializers.charfield(source='course.course_img')

level = serializers.charfield(source='course.get_level_display')

# m2m

recommends = serializers.serializermethodfield()

class meta:

model = models.coursedetail

fields = ['course','title','img','level','slogon','why','recommends']

def get_recommends(self,obj):

# 獲取推薦的所有課程

queryset = obj.recommend_courses.all()

return [ for row in queryset]

# view

# apiview

# genericapiview

from rest_framework.viewsets import genericviewset,viewsetmixin,modelviewset

class courseview(viewsetmixin,apiview):

def list(self,request,*args,**kwargs):

"""課程列表介面

:param request:

:param args:

:param kwargs:

:return:

"""ret =

try:

queryset = models.course.objects.all()

ser = courseserializer(instance=queryset,many=true)

ret['data'] = ser.data

except exception as e:

ret['code'] = 1001

ret['error'] = '獲取課程失敗'

return response(ret)

def retrieve(self,request,*args,**kwargs):

"""課程詳細介面

:param request:

:param args:

:param kwargs:

:return:

"""ret =

try:

# 課程id=2

pk = kwargs.get('pk')

# 課程詳細物件

obj = models.coursedetail.objects.filter(course_id=pk).first()

ser = coursedetailserializer(instance=obj,many=false)

ret['data'] = ser.data

except exception as e:

ret['code'] = 1001

ret['error'] = '獲取課程失敗'

訪問  

返回結果如下

訪問  

1
返回結果如下

後其身而身先,外其身而身存

後其身而身先,外其身而身存。世間大多之事,莫過如此啊。天龍八部 中慕容復一心一意以祖傳訓誡為重,欲興復大燕的國家大業,為人心機頗重 城府極深。為了達成目的不擇手段,眾叛親離,聲名掃地,千方百計條條不成,最後屢屢受挫而發瘋。天龍八部 中虛竹,恰恰相反,好多事都是無意為之,結果意外受傳了多項絕世武功,進...

AuthorizationFilter身份驗證資訊

最近被跨域問題搞得焦頭爛額,在前端ajax往伺服器端傳送請求的時候,我嘗試在請求報文header裡面自定義請求頭 ajax success function data,status error function error 後端伺服器 if actioncontext.request.headers...

容器 模版小試

include include include include using namespace std template typename iterator traits value type findmost iterator left,iterator right else 遍歷 map ite...