class NewsSerializer(serializers.ModelSerializer): category = NewsCategorySerializer() author = UserSerializer() class Meta: model = News fields = ('id','title','desc','thumbnail','pub_time','category','author')
class NewsSerizlizer(serializers.ModelSerializer): category = NewsCategorySerializer() author = UserSerializer() class Meta: model = News # 此时首页的地方用不到内容 所以不添加 减少网络传输 因为内容一般体积较大 fields = ('id', 'title', 'desc', 'thumbnail', 'pub_time', 'category', 'author')
class NewspubSerializer(serializers.ModelSerializer): category = AddNewsCategorySerializer() author = UserSerializer() class Meta: model = NewsPub fields = [ 'id', 'title', 'category', 'desc', 'thumbnail', 'pub_time', 'author', 'display' ]
class NewsSerializer(serializers.ModelSerializer): # category和author是外键,我们还需要进行一层序列化 category = NewsCategorySerializer() author = UserSerializer() class Meta: model = News # 在首页新闻列表不需要获取新闻的正文,所以没有加上content fields = ('id', 'title', 'desc', 'thumbnail', 'pub_time', 'category', 'author')
class NewsSerializer(serializers.ModelSerializer): category = NewsCategorySerializer() # 要想序列化category。必须导入category的序列化 author = UserSerializer() class Meta: model = News # 执行要序列化的模型 # 指定序列化的字段,其中category、author的序列化需要先提前序列化 fields = ('id', 'title', 'desc', 'thumbnail', 'pub_time', 'category', 'author')
class CommentSerizlizer(serializers.ModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ('id', 'content', 'author', 'pub_time') # class BannerSerializer(serializers.ModelSerializer): # class Meta: # model = Banner # fields = ('id','image_url','priority','link_to')
class NewsSerializer(serializers.ModelSerializer): category = NewsCategorySerializer() author = UserSerializer() class Meta: # 对新闻模型序列化 model = News # 需要序列化哪些字段 # 内容字段是不需要的,因为内容字段又多又长 # category和auth需要单独去写(它们只是外键,在News模型中,category和auth真正保存的只是id) # 而如category我们不仅仅想要id,还想要如name # 由于author在zfzauth这个app下面,因此在xfzauth下写一个序列化(当然不一定非要放到里面来写,只不过放到xfzauth里来写显得更整洁) fields = ('id','title','desc','thumbnail','pub_time','category','author')
class CommentSerializer(serializers.ModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ('id', 'content', 'author', 'pub_time')
class NewsCommentSerializer(serializers.ModelSerializer): commentator = UserSerializer() class Meta: model = NewsComment fields = ['id', 'content', 'pub_time', 'commentator']
class CommentSerializer(serializers.HyperlinkedModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ['id', 'content', 'author', 'pub_time']
class CommentSerizlizer(serializers.ModelSerializer): author = UserSerializer() class Meta: model = Comment fields = ("id", "content", "author", "pub_time")