Django:如何构建一个自定义表单窗口小部件? [英] Django: How to build a custom form widget?

查看:615
本文介绍了Django:如何构建一个自定义表单窗口小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到关于如何编写自定义小部件的文档。

I am having a difficult time finding documentation on how to write a custom widget.

我的问题是:


  • 如果我建立一个自定义窗口小部件,可以等效地用于管理界面还是正常窗体?

  • 如果我想允许用户编辑一个项目列表,我应该子类化什么小部件?

  • 什么小部件方法负责从用户的输入回到数据模型?

谢谢。

推荐答案

你是正确的Django不,提供关于这个具体主题的文档。我建议您查看 django.forms.widgets (我将从下面的模块中引用类)。

You're right in that Django doesn't supply documentation on this specific topic. I advise you to look at the builtin widgets in django.forms.widgets (I'll reference classes from that module below).


如果我构建一个自定义的窗口小部件,它可以等效地用于管理界面还是用于正常窗体?

If I build a custom widget, can it be used equivalently for the admin interface or for normal forms?

管理员覆盖一些窗口小部件请参阅 django.contrib.admin。 options.FORMFIELD_FOR_DBFIELD_DEFAULTS )。您可以继承 ModelAdmin 并更改 formfield_overrides 属性,但是我从来没有使用 ModelAdmin 所以我无法在这里帮助...

Admin overrides some widgets (see django.contrib.admin.options.FORMFIELD_FOR_DBFIELD_DEFAULTS). You can probably subclass ModelAdmin and change the formfield_overrides attribute, but I have never done anything with ModelAdmin so I can't help here...


如果我想允许用户编辑项目列表,我应该哪个小部件?我需要什么方法来覆盖/实现?

If I want to allow the user to edit a list of items, what widget should I subclass? What methods of the widget do I need to override/implement?

您的窗口小部件可能与默认窗口小部件没有任何共同之处(与选择如果有的话)!从 Widget 中的子类,如果您发现任何常见的内置模式,您仍然可以稍后更改。

Your widget probably doesn't have anything in common with the default widgets (with Select if any?!). Subclass from Widget and if you find any common pattern with builtins, you can still change it later.

以下方法:


  • render(self,name,value,attrs = None)

查看 Input.render 为一个简单的例子。它还支持包含在HTML中的用户定义的属性。您可能还需要添加id属性,有关如何执行此操作,请参阅 MultipleHiddenInput.render 。直接输出HTML时,不要忘记使用 mark_safe 。如果您有一个相当复杂的小部件,您可以使用模板呈现( example )。

Check out Input.render for a simple example. It also supports user-defined attributes that are included in the HTML. You may also want to add "id" attributes, see MultipleHiddenInput.render on how to do that. Don't forget to use mark_safe when outputting HTML directly. If you have a rather complex widget you can use template rendering (example).

_has_changed(self,initial,data)

可选。用于管理员记录有关更改内容的消息。

Optional. Used in admin to log messages about what was changed.


什么小部件方法负责从用户的输入回到数据模型?

What widget method is responsible for going from the user's input back to the data model?

这与widget无关 - Django不知道什么小部件在较早的请求中被使用。它只能使用从表单发送的表单(POST)。因此,字段方法 Field.to_python 用于将输入转换为Python数据类型(可能会引发 ValidationError ,如果输入无效)。

That has nothing to do with the widget - Django can't know what widget was used in an earlier request. It can only use the form (POST) data sent from the form. Therefore, the field method Field.to_python is used to convert input to the Python data type (may raise ValidationError if the input is invalid).

这篇关于Django:如何构建一个自定义表单窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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