我收到以下消息,此用法在之前工作。我已经删除了下面的.delay函数,只是为了生成消息,因为这是一项任务,但通常看起来像 ProcessRequests.delay。(批处理)
对象._ 新 _()不带参数
if request.method == 'POST':
batches = Batch.objects.for_user_pending(request.user)
for batch in batches:
ProcessRequests(batch) #ProcessRequests.delay is normally used here
batch.complete_update()
任务:
class ProcessRequests(Task):
name = "Request to Process"
max_retries = 1
default_retry_delay = 3
def run(self, batch):
for e in Contact.objects.filter(contact_owner=batch.user, group=batch.group):
msg = Message.objects.create(
recipient_number=e.mobile,
content=batch.content,
sender=e.contact_owner,
billee=batch.user,
sender_name=batch.sender_name
)
gateway = Gateway.objects.get(pk=2)
msg.send(gateway)
完整错误:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/sms/process
Django Version: 1.5.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.humanize',
'south',
'sms',
'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'async_messages.middleware.AsyncMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/Users/user/Documents/workspace/s/django-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/user/Documents/workspace/s/django-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
25. return view_func(request, *args, **kwargs)
File "/Users/user/Documents/workspace/s/sms/views.py" in process_all
214. ProcessRequests(batch)
Exception Type: TypeError at /sms/process
Exception Value: object.__new__() takes no parameters
答案 0 :(得分:2)
看起来您正在尝试使用参数实例化ProcessRequests
类,但该类没有带参数的构造函数,因此出错。
我认为你只需要改变......
ProcessRequests(batch)
...到...
ProcessRequests().run(batch)
...但它是否能满足您的需求取决于Task
类的定义。