django前后端写代码常出的问题

问题一:
class NewsSerializer(serializers.ModelSerializer):
    # class Meta:
      model = News
      fields = '__all__'

class ShowNews(APIView):
    def get(self,request):
        news = News.objects.all()
        ser = NewsSerializer(news,many=True)
        return Response(ser.data)
序列化展示的时候在这里没有写 class Meta:,所以会报一个错

在这里插入图片描述
在这里插入图片描述

问题二:

一对多关系中,序列化如果不继承,在展示的时候会没有效果,具体代码如下:

<template>
    <div>
      <table border="1">
        <tr>
          <th>标题</th>
          <th>价格</th>
          <th>作者</th>
          <th>操作</th>
        </tr>
        <tr v-for="item in datalist">
          <td>{{ item.title }}</td>
          <td>{{ item.price }}</td>
          <td>{{ item.author_id.username}}</td>  # 展示外键在展示图书的时候展示作者姓名  而不是作者id
          <td><button @click="zhuan(item.id)">转移</button></td>
        </tr>
      </table>
      <div>
        <button v-show="lastpage" @click="mychange(lastpage)">上一页</button>
        <button v-for="index in all" @click="mychange(index)">{{ index }}</button>
        <button v-show="nextpage" @click="mychange(nextpage)">下一页</button>
      </div>
    </div>
</template>

<script>
  import axios from 'axios'
    export default {
        data(){
          return{
            datalist:[],
            total:0,
            cur:1,
            lastpage:0,
            nextpage:0,
            all:0
          }
        },
      mounted() {
          this.mychange(1);
          axios({
            url:'http://localhost:8000/showbook/',
            method:'get',
          }).then(res=>{
            this.datalist = res.data.data;
          })
      },
      methods:{
          zhuan(id){
            this.$router.push({path:'/zhuan',query:{id:localStorage.getItem('userid'),bid:id}})
          },
          mychange(page){
            axios({
              url:'http://localhost:8000/showbook/',
              method:'get',
              params:{page:page,size:1}
            }).then(res=>{
              this.datalist = res.data.data;
              if (page == 1){
                this.lastpage = 0;
              } else{
                this.lastpage = page - 1
              }
              this.all = Math.ceil(res.data.total / this.cur);
              if (page == this.all){
                this.nextpage = 0
              } else{
                this.nextpage = page + 1;
              }
            })
          }
      }
    }
</script>

class BookSerializer(serializers.ModelSerializer):
    # author_id = UserSerializer()
    class Meta:
        model = Book
        fields = '__all__'

在这里插入图片描述
在这里插入图片描述

问题三:

使用反序列化添加时,必须使用post添加,如果使用get添加,会加不进去的

在这里插入图片描述

如果是get,每天访问添加不会走添加的逻辑,直接就是添加失败

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值