javascript - django为什么在api视图里就找不到数据呢?

查看:95
本文介绍了javascript - django为什么在api视图里就找不到数据呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我在views.py里面打印这个对象都能成功打印:

def inherit(request):
  print(Inheritinfo.objects.filter()[0])     #这里都可以成功打印出来

  return render(request, 'inherit.html')
  

但是为什么在api视图里面就报错呢?
报错:AttributeError: 'function' object has no attribute 'objects'

   class InheritinfoSerializer(serializers.ModelSerializer):
     class Meta:
         model = Inheritinfo
         fields = '__all__'
         depth = 1

@api_view(['GET', 'POST'])
def Inheritinfo(request):
  if request.method == 'GET':
      print(Inheritinfo.objects.filter()[0]) #在这一句报的错
      postinfo = Inheritinfo.objects.filter()[0]
      serializer = InheritinfoSerializer(postinfo)
      return Response(serializer.data)

解决方案

你的函数名是Inheritinfo,model又是Inheritinfo,不知道给函数改个名吗

这篇关于javascript - django为什么在api视图里就找不到数据呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆