在Django中,如何使用ModelForm作为值列表将多个表单域保存到单个模型字段? [英] In Django, how do you save multiple form fields into a single model field as a list of values using a ModelForm?
问题描述
从django.db导入模型
class MyModel(models.Model):
...
owner = models.CharField(max_length = 255,blank = False)
其中所有者
是一个管道分隔的电子邮件地址列表。
在页面中,有多个< input>
具有相同名称
的字段,因此服务器将获取数组中的值。
表单的HTML代码是手工完成的,不使用内置模板(例如 form.as_p
)。
Django的 ModelForm
可以处理这样的事情吗?
在检索模型时,需要处理数据转换的正确位置是什么(我必须自定义 models.Manager
?)和保存它(其中 save()
方法我覆盖?模型
或 ModelForm
s)?
- 更新为澄清 -
在数据库中:
+ ----- + --------------------------- + ----- +
| ... |业主| ... |
+ ----- + --------------------------- + ----- +
| ... | a@a.com | b@b.com | c@c.com| ... |
+ ----- + --------------------------- + ----- +
表单:
< form ...>
...
< input type =textname =owner/>
< input type =textname =owner/>
< input type =textname =owner/>
...
< / form>
您可以使用 forms.MultiValueField
和 forms.MultiWidget
。 MultiValueField
知道您的模型的一个字段包含多个离散值,并且 MultiWidget
窗口小部件知道如何呈现每个离散值。他们将处理为您的字段创建唯一的名称,以便它们可以从 POST
值重建表单,您不必重写提供实现的任何表单方法,即您不需要覆盖 save()
。
正如你所看到的,官方文档有点薄关于这个话题。我建议您仔细查看代码,以了解一切如何有线以及调用时间,因为您的要求总是需要一个独特的解决方案,即该字段包含多少个电子邮件?恰恰是两个,所以我可以手动指定两个 CharField
小部件,或者它将在0-2之间,所以我必须根据传递的值动态构建一切?如果您需要一个很好的例子作为开始参考,请查看这个SO问题,其中包含一个干净且易于遵循的实现。
理想情况下,您将需要一个 PipedEmailAddressesField
将 CharField
作为您的模型的字段,知道它存储管道的电子邮件地址。它应该能够构造它自己适当的 MultiValueField
表单字段,这应该反过来构造它自己适当的 MultiWidget
字段。
Given:
from django.db import models
class MyModel(models.Model):
...
owners = models.CharField(max_length=255, blank=False)
where owners
is a pipe-separated list of email addresses.
In the page, there are multiple <input>
fields with the same name
, so the server gets the values in an array.
The HTML code for the form was done by-hand and doesn't use built-in templates (such as form.as_p
).
Can Django's ModelForm
s handle something like this?
What is the proper location to handle the data transformation, both when retrieving the model (do I have to make a custom models.Manager
?) and when saving it (which save()
method do I override? the Model
's or the ModelForm
's)?
--UPDATE FOR CLARIFICATION--
In the database:
+-----+---------------------------+-----+
| ... | owners | ... |
+-----+---------------------------+-----+
| ... | "a@a.com|b@b.com|c@c.com" | ... |
+-----+---------------------------+-----+
The form:
<form ... >
...
<input type="text" name="owners" />
<input type="text" name="owners" />
<input type="text" name="owners" />
...
</form>
You can use forms.MultiValueField
and forms.MultiWidget
. MultiValueField
knows that your model's one field contains multiple discrete values and the MultiWidget
widget knows how each discrete value should be presented. They'll handle creating unique names for your fields so that they can reconstruct the form from POST
values and you won't have to override any form methods that provide implementation, i.e. you won't have to override save()
.
As you can see, the official docs are a bit thin on the topic. I suggest you take a peek at the code to figure out how everything is wired and what gets called when, since your requirements are always going to need a unique solution, i.e: how many emails will the field contain? Exactly two, so I can specify two CharField
widgets by hand, or is it going to be between 0-2, so I have to dynamically construct everything depending on the value passed? If you need a good example as a starting reference, check out this SO question which contains a clean and easy to follow implementation.
Ideally, you'll want to have a PipedEmailAddressesField
that extends CharField
as your model's field, one that knows that it's storing piped email addresses. It should be able to construct it's own appropriate MultiValueField
form field which should in turn construct it's own appropriate MultiWidget
field.
这篇关于在Django中,如何使用ModelForm作为值列表将多个表单域保存到单个模型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!