Django:通过选择对象的名称从管理员运行脚本

时间:2021-08-31 20:27:22

I am using Django 1.8 and Python 2.7.8 to create a web interface with MySQL databases, I can just use the Admin page to manipulate different tables. Suppose I have a student object with fields: name, id and gender. How to achieve some function like this:

我使用Django 1.8和Python 2.7.8创建一个带有MySQL数据库的Web界面,我可以使用Admin页面来操作不同的表。假设我有一个带有字段的学生对象:姓名,身份和性别。如何实现这样的功能:

  • select student's name: just avoid typo in names
  • 选择学生的名字:只需避免名字错字
  • click a button like "Run script"
  • 点击“运行脚本”之类的按钮
  • trigger a Python program to run
  • 触发Python程序运行

I only need output from that Python program like: success or failure.

我只需要从Python程序输出:成功或失败。

Thanks very much!

非常感谢!

1 个解决方案

#1


1  

What you are after is admin actions. You write a custom function in the admin, and assign the function name to the admin_actions property.

你所追求的是管理行动。您在admin中编写自定义函数,并将函数名称分配给admin_actions属性。

def incomplete_tasks(modeladmin, request, queryset):
    queryset.update(completed=False)
incomplete_tasks.short_description = 'Mark as Not Complete'

class TaskAdmin(admin.ModelAdmin):
    list_display = ['title', 'completed']
    ordering = ['created']
    actions = [incomplete_tasks,]

admin.site.register(Task, TaskAdmin)

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/

https://godjango.com/78-custom-django-admin-actions/

https://godjango.com/78-custom-django-admin-actions/

#1


1  

What you are after is admin actions. You write a custom function in the admin, and assign the function name to the admin_actions property.

你所追求的是管理行动。您在admin中编写自定义函数,并将函数名称分配给admin_actions属性。

def incomplete_tasks(modeladmin, request, queryset):
    queryset.update(completed=False)
incomplete_tasks.short_description = 'Mark as Not Complete'

class TaskAdmin(admin.ModelAdmin):
    list_display = ['title', 'completed']
    ordering = ['created']
    actions = [incomplete_tasks,]

admin.site.register(Task, TaskAdmin)

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/actions/

https://godjango.com/78-custom-django-admin-actions/

https://godjango.com/78-custom-django-admin-actions/