[英]Django - Admin - on form change
I have a general question to the django-admin.我有一个向 django-admin 提出的一般性问题。
Is it possible to react on form changes?是否可以对表单更改做出反应?
I have a select field in my django-admin detail site.我的 django-admin 详细信息站点中有一个选择字段。 Whenever i change the data from the select field, i want to change fields which are readonly.每当我更改选择字段中的数据时,我都想更改只读字段。
Anybody ever dealed with this issue?有没有人处理过这个问题?
Thanks and Greetings谢谢和问候
My two cents:我的两分钱:
As any other guys said it is a javascript
work.正如任何其他人所说,这是一项javascript
工作。 In admin pages Django pases jquery
.在管理页面中,Django 传递jquery
。 It is called django.jQuery
.它被称为django.jQuery
。 So basilly you would do what @Ashfaq suggested.所以你会按照@Ashfaq 的建议去做。 You will create a custom_script.js
and added to the Media
metaclass.您将创建一个custom_script.js
并添加到Media
元类。
Basically(as @Ashfaq):基本上(如@Ashfaq):
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ("js/custom_script.js",)
and custom_script.js
will be something like this(assuming that your select
field is called id_category
):和custom_script.js
将是这样的(假设您的select
字段称为id_category
):
django.jQuery( document ).ready(function() {
console.log( "ready!" );
django.jQuery('#id_category').change(function() {
alert( "Handler for #id_category was called." );
});
});
The ready
function will guarantee the handler is getting set. ready
函数将保证处理程序正在设置。
I think the thing that will work here is to add jQuery + your custom javascript and play with the events / clicks what-ever you want with elements.我认为在这里可以工作的是添加 jQuery + 您的自定义 javascript 并使用您想要的元素来处理事件/点击。
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ("js/custom_script.js",)
in custom_script you can add click or change events as you want.在 custom_script 中,您可以根据需要添加单击或更改事件。
Found a great answer by Abhijith K in another SO Q&A:在另一个 SO Q&A 中找到了 Abhijith K 的一个很好的答案:
How can i add a onchange js event to Select widget in Django? 如何将 onchange js 事件添加到 Django 中的 Select 小部件?
reciept=forms.ChoiceField(reciept_types, widget = forms.Select(attrs = {'onchange': "myFunction();"}))
To be clear, this is what you add within your widget definition: attrs = {'onchange': "myFunction();"}
to define which JS function will be called, in this when an onchange
event is triggers.需要明确的是,这是您在小部件定义中添加的内容: attrs = {'onchange': "myFunction();"}
来定义将调用哪个 JS 函数,此时触发onchange
事件。
In the ModelAdmin you can then define a JavaScript file that you want to have access to, where you can define your function "myFunction()":在 ModelAdmin 中,您可以定义一个您想要访问的 JavaScript 文件,您可以在其中定义您的函数“myFunction()”:
@admin.register(AnswerTree)
class AnswerTreeAdmin(ModelAdmin):
form = AnswerTreeForm
...
class Media:
js = ("admin_js/answertree_modeladmin.js",)
Django docs on defining assets (Like JavaScript & CSS) on your ModelAdmin: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions在 ModelAdmin 上定义资产(如 JavaScript 和 CSS)的 Django 文档: https ://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions
What is also useful, I found out that you can access the value of the input by using attrs = {'onchange': "myFunction(this.value);"}
, note that I am passing the argument this.value
here now.还有什么有用的,我发现你可以通过使用attrs = {'onchange': "myFunction(this.value);"}
来访问输入的值,注意我现在在这里传递参数this.value
。
Example of the JavaScript function definition: JavaScript 函数定义示例:
myFunction = (value) => {
console.log(value)
}
OR
myFunction(value) {
console.log(value)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.