Django:如何从管理界面调用管理自定义命令执行? [英] Django: How to call management custom command execution from admin interface?
问题描述
请参阅从代码中执行管理命令,
它们是从Django管理界面调用此命令执行代码的方法吗?
Is their a way to call this command execution code from the django admin interface?
我有一个自定义命令来对数据库进行一些定期更新,该数据库已安排为cron 。 Cron工作正常。我需要在需要时从管理界面手动更新数据库。
I've a custom command to do some periodic update of database which has been scheduled as cron. The cron is working fine. I need to update the database manually from the admin interface whenever needed.
推荐答案
更新:只需在python代码中的任意位置调用函数 call_command('compilemessages')
即可运行任何管理命令。注意:从呼叫者的角度来看,这显然是一个阻止过程。通过下面的ajax示例,您可以拥有一种非阻塞/异步用户体验的形式。根据后端的实现,您可以进一步提高隔离级别。
update: you can run any management command simply by calling the function call_command('compilemessages')
from anywhere within your python code. Note: this obviously is a blocking process from the caller's perspective. With the ajax example below you can have a form of non-blocking/asynchronous user experience. Depending on the backend implementation you can take this isolation level further.
示例:
from django.core.management import call_command
call_command('compilemessages')
如果该任务绑定到当前在admin中查看的对象,那么一种不错的方法可能是在单击按钮时实现一个由ajax脚本调用的额外视图。额外的视图可以选择包装为芹菜任务,例如
If the task is bound to the object currently viewed in the admin, a nice way might be to implement an extra view called by an ajax script when clicking a button. The extra view could optionally be wrapped as a celery task, e.g.
models.py
class Foo(models.Model):
# fields...
def my_task_init(self):
return mark_safe("<img class='loading' src='/static/img/loading.gif' alt='loading' style='display:none;' /><a data-identifier='task_%i' class='task'><img src='/static/img/process.png' style='cursor:pointer;' /></a>") % self.id
my_task_init.allow_tags = True
my_task_init.short_description = _(u"Execute Task")
admin.py
class FooAdmin(admin.ModelAdmin):
list_display = ['other_field', 'my_task_init']
class Media:
js = (
'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js',
'/static/js/admin_tasks.js',
)
def get_urls(self):
urls = super(FooAdmin, self).get_urls()
extra_urls = patterns('',
(r'^my-task/$', self.admin_site.admin_view(self.parse_view))
)
return extra_urls + urls
# optionally decorated by celery
def task_view(self, request):
if not request.is_ajax():
raise Http404
task_id = request.GET.get('task_id')
# your logic
return HttpResponse('Success')
admin_tasks.js
$(document).ready(function (){
$('.task').click(function(){
var image = $(this).find('img'),
loading = $(this).parent().find('.loading'),
task_id = $(this).data('identifier').replace('task_', '');
$.ajax({
type: "GET",
data: ({'task_id': task_id}),
url: "/admin/app/model/my-task/",
beforeSend: function() {
image.hide();
loading.show();
},
statusCode: {
200: function() {
loading.hide();
image.attr('src', '/static/img/success.png');
image.show();
},
404: function() {
loading.hide();
image.attr('src', '/static/img/error.png');
image.show();
},
500: function() {
loading.hide();
image.attr('src', '/static/img/error.png');
image.show();
}
}
});
});
});
如果您要启动未绑定的任务,则可以覆盖模板元素或添加一些html
If you're trying to initiate an unbound task you could just override a template element or add some html.
这篇关于Django:如何从管理界面调用管理自定义命令执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!