#####################################
from api import models from utils.response_dict import BaseResponse from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet, ViewSetMixin from api.serializers.serialize_article import ArticleSerializers, ArticleDetailSerializers from api.views.auth import YcAuth class DeepNews(ViewSetMixin, APIView): authentication_classes = [YcAuth,] def list(self, request, *args, **kwargs): """ 推文列表内容 :param request: :param args: :param kwargs: :return: """ ret = BaseResponse() try: article_list = models.Article.objects.all() serialized_art_list = ArticleSerializers(instance=article_list, many=True) ret.data = serialized_art_list.data except Exception as e: ret.code = 1001 ret.error = '未获取到资源' return Response(ret.dict) def retrieve(self, request, *args, **kwargs): ret = BaseResponse() try: pk = kwargs.get('pk') obj = models.Article.objects.filter(pk=pk).first() ser = ArticleDetailSerializers(instance=obj, many=False) ret.data = ser.data except Exception as e: ret.data = 1001 ret.error = '未获取到资源' return Response(ret.dict)
############################################
from rest_framework import serializers from api.models import Article class ArticleSerializers(serializers.ModelSerializer): """ Article表的序列化 """ source = serializers.CharField(source="source.name") article_type = serializers.CharField(source="get_article_type_display") position = serializers.CharField(source='get_position_display') class Meta: model = Article fields = ["title", "source", "article_type", 'head_img', 'brief', 'pub_date', 'comment_num', 'agree_num', 'view_num', 'collect_num', 'position'] class ArticleDetailSerializers(serializers.ModelSerializer): """ ArticleDetail表的序列化 """ class Meta: model = Article fields = ['title', 'pub_date', 'agree_num', 'view_num', 'collect_num', 'comment_num', 'source', 'content', 'head_img']
#############################################
#############################################
#############################################