问题描述
使用django模型和django-apscheduler,当我将模型保存在管理界面中时,我正在尝试创建作业。
我的模特:
def test():
logger.debug("This is my test job running")
class Job(models.Model):
COMMAND_TYPES = (
('','-------'),(0,'do thing 2'),(1,'do thing 1'),)
command = models.IntegerField(choices=COMMAND_TYPES,default='')
interval = models.IntegerField(default=600,validators=[MinValueValidator(0),MaxValueValidator(3600)] )
enabled = models.BooleanField(default=False)
def save(self,*args,**kwargs):
scheduler.add_job(test,'interval',seconds=self.interval,id=self.COMMAND_TYPES[self.command+1][1],max_instances=1,replace_existing=True,coalesce=True)
super(Job,self).save(*args,**kwargs)
在其他地方手动完成后,添加作业即可完成。
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/exception.py",line 47,in inner
response = get_response(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py",line 179,in _get_response
response = wrapped_callback(request,*callback_args,**callback_kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/options.py",line 614,in wrapper
return self.admin_site.admin_view(view)(*args,**kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/utils/decorators.py",line 130,in _wrapped_view
response = view_func(request,**kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/views/decorators/cache.py",line 44,in _wrapped_view_func
response = view_func(request,**kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/sites.py",line 233,in inner
return view(request,**kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/options.py",line 1656,in change_view
return self.changeform_view(request,object_id,form_url,extra_context)
File "/usr/local/lib/python3.7/dist-packages/django/utils/decorators.py",line 43,in _wrapper
return bound_method(*args,line 1534,in changeform_view
return self._changeform_view(request,extra_context)
File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/options.py",line 1587,in _changeform_view
self.log_change(request,new_object,change_message)
File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/options.py",line 832,in log_change
change_message=message,File "/usr/local/lib/python3.7/dist-packages/django/contrib/admin/models.py",line 35,in log_action
change_message=change_message,File "/usr/local/lib/python3.7/dist-packages/django/db/models/manager.py",line 85,in manager_method
return getattr(self.get_queryset(),name)(*args,**kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/query.py",line 447,in create
obj.save(force_insert=True,using=self.db)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py",line 751,in save
force_update=force_update,update_fields=update_fields)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py",line 789,in save_base
force_update,using,update_fields,File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py",line 892,in _save_table
results = self._do_insert(cls._base_manager,fields,returning_fields,raw)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py",line 932,in _do_insert
using=using,raw=raw,line 1249,in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/sql/compiler.py",line 1395,in execute_sql
cursor.execute(sql,params)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py",line 98,in execute
return super().execute(sql,line 66,in execute
return self._execute_with_wrappers(sql,params,many=False,executor=self._execute)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py",line 75,in _execute_with_wrappers
return executor(sql,many,context)
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/utils.py",line 78,in _execute
self.db.validate_no_broken_transaction()
File "/usr/local/lib/python3.7/dist-packages/django/db/backends/base/base.py",line 448,in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
我无法在线上查看错误django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
。如果是由于数据库忙碌或其他原因导致的问题,怎么等到事务结束?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)