如何修改Django中某种类型的所有内置表单字段的默认窗口小部件? [英] How do you modify the default widget for all builtin form fields of a certain type in Django?

查看:163
本文介绍了如何修改Django中某种类型的所有内置表单字段的默认窗口小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何更改ModelForm中所有Django日期字段的默认窗口小部件的后续操作?

假设您拥有非常多的型号(例如A-ZZZ),这些模型随着其他开发人员的输入而不断增长,这些开发人员无法控制,您想要更改方式输入所有日期字段(即使用jQueryUI)。什么是确保使用该新窗口小部件填写所有日期字段的最佳方法?

Suppose you have a very large number of models (e.g. A-ZZZ) that is growing with the input of other developers that are beyond your control, and you want to change the way all date fields are entered (i.e. by using jQueryUI). What's the best way to ensure that all date fields are filled out using that new widget?

引用问题的一个建议是:

One suggestion from the cited question was:

def make_custom_datefield(f):
   if isinstance(f, models.DateField):
       # return form field with your custom widget here...
   else:
       return f.formfield()

class SomeForm(forms.ModelForm): 
   formfield_callback = make_custom_datefield

   class Meta:
       # normal modelform stuff here...

但是,这可能在哪里你没有明确的ModelForm,但url模式直接来自模型?即你的url配置是likeo:

However, is this possible to do where you don't have explicit ModelForm's, but url patterns come from models directly? i.e. your url config is likeso:

url(r'^A/?$', 'list_detail.object_list', SomeModelA)

其中SomeModelA是Django在后台变成ModelForm的一个模型(不是一个表单)。

where SomeModelA is a model (not a form) that's turned into a ModelForm by Django in the background.

目前,在我的系统中,每个模型都没有窗体。创建表单的唯一要点是添加前面解决方案中建议的formfield_callback,但这与DRY原则相反,并且容易出错,而且劳动密集。

At present in my system there are no Forms for each Model. The only point of creating forms explicitly would be to add the formfield_callback suggested in the prior solution, but that goes against DRY principles, and would be error prone and labour intensive.

我已经考虑了(如最后一个线程中的建议)创建我自己的字段,它有一个特殊的小部件,而不是内置的。这不是劳动密集型,但它可能会受到错误(没有一个好的grep无法解决,但是)。

I've considered (as suggested in the last thread) creating my own field that has a special widget and using that instead of the builtin. It's not so labour intensive, but it could be subject to errors (nothing a good grep couldn't fix, though).

建议和想法是赞赏。

推荐答案

听起来你想在这个项目范围内做这个工作(即:在某些情况下你不想这样做,您运行的应用程序中的所有情况)。

It sounds like you want to do this project-wide (ie: you're not trying to do this in some cases, but in ALL cases in your running application).

一种可能性是替换DateField类本身的widget属性。您需要在某些中央位置执行此操作...某些保证由django应用程序的每个运行实例加载。中间件可以帮助这个。否则,只需将其放在您的应用程序的 __ init __ 文件中。

One possibility is to replace the widget attribute of the DateField class itself. You would need to do this in some central location... something that is guaranteed to be loaded by every running instance of the django app. Middleware can help with this. Otherwise, just put it in the __init__ file of your app.

您要做的是重新分配form.DateField类的widget属性本身。当创建一个新的DateField时,Django会检查代码是否在字段属性定义中指定了任何特定的小部件。如果没有,它将使用默认的DateField。我假设如果你的场景中的一个用户真的定义了一个特定的小部件,那么尽管改变了你的全局API,但是你想要遵守。

What you want to do is re-assign the widget property for the forms.DateField class itself. When a new DateField is created, Django checks to see if the code specifies any particular widget in the field property definition. If not, it uses the default for DateField. I'm assuming that if a user in your scenario really defined a particular widget, you'd want to honour that despite the change to your global API.

尝试这样做一个强制默认的其他小部件的例子...在这种情况下,一个HiddenInput:

Try this as an example of forcing the default to some other widget... in this case a HiddenInput:

from django import forms
forms.DateField.widget = forms.HiddenInput

class Foo(forms.Form):
    a = forms.DateField()

f = Foo()
print f.fields['a'].widget
# results in <django.forms.widgets.HiddenInput object at 0x16bd910>

这篇关于如何修改Django中某种类型的所有内置表单字段的默认窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆