Django模型:TypeError:“ Manager”对象不可调用

时间:2019-07-30 08:09:40

标签: python django django-models django-filter

我正在使用Django开发Rest API。我有这个模型:

from django.db import models
from devices.models import Device

class Command(models.Model):
    id = models.AutoField(primary_key=True, blank=True)
    command_name = models.CharField(max_length=70, blank=False, default='')
    time = models.DateTimeField(auto_now_add=True, blank=True)
    timeout = models.TimeField(blank=True)
    command_status= models.IntegerField(blank=False)
    tracking = models.IntegerField(blank=False)
    result_description = models.CharField(max_length=200, blank=False, default='')
    ssid = models.CharField(max_length=31, blank=False, default='')
    device = models.ForeignKey(Device, on_delete=models.CASCADE)

在视图中,我有这部分代码:

@csrf_exempt 
def command_detail(request, rssid):
    try: 
    print("Heeeeeeeeeeeeeeeeeeeeeeeeelllo 1111 rssid: ", rssid)
    commands=Command.objects().filter(ssid=rssid)
    print("Heeeeeeeeeeeeeeeeeeeeeeeeelllo 2222")
    command=commands[0]
    except Command.DoesNotExist: 
    return HttpResponse(status=status.HTTP_404_NOT_FOUND) 

如果执行我的服务器,它可以正常工作而没有任何错误,但是如果我尝试使用curl来测试相应的服务器,例如:curl -X POST http://127.0.0.1:8000/api/commands/cmdssid1?command_status=40我得到的curl结果是一个很大的html,指示服务器中出现内部错误,并且在Django部分中,我得到了这些痕迹:

July 30, 2019 - 08:00:01
Django version 1.10.1, using settings 'project-name.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
('Heeeeeeeeeeeeeeeeeeeeeeeeelllo 1111 rssid: ', u'cmdssid1')
Internal Server Error: /api/commands/cmdssid1
Traceback (most recent call last):
  File "/path-to-home/.local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/path-to-home/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/path-to-home/.local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/path-to-home/.local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/path-to-project/command/views.py", line 56, in command_detail
    commands=Command.objects().filter(ssid=rssid)
TypeError: 'Manager' object is not callable
[30/Jul/2019 08:00:06] "POST /api/commands/cmdssid1?command_status=40 HTTP/1.1" 500 74917

我进行了很多搜索,并进行了一些调试研究,但实际上并不清楚解决此问题的主要原因。

有人知道错误的原因是什么,我该如何解决?

1 个答案:

答案 0 :(得分:2)

代替此行:

commands=Command.objects().filter(ssid=rssid)

使用以下行

commands=Command.objects.filter(ssid=rssid)

因为如错误所述,Manager对象不可调用。您需要在不使用方括号的情况下使用它。