直接在模型中存储django forms.MultipleChoiceField [英] Store django forms.MultipleChoiceField in Models directly

查看:683
本文介绍了直接在模型中存储django forms.MultipleChoiceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有如下定义的选择:

  choices =(('1','a'),
('2','b'),
('3','c'))

一个表单,它在MultipleChoiceField中呈现和输入这些值,

  class Form1(forms.Form): 
field = forms.MultipleChoiceField(choices = choices)

什么是正确的存储方式



我当然可以循环使用 forms.cleaned_data [ 'field'] 并获得一个适合 models.CommaSeperatedIntegerField 的值。



再次,每次我检索这些值,我将不得不循环并转换为选项。



我认为有更好的方式这样做,因为这样,我正在重新实现CommaSeparateIntegerField应该做的功能。

解决方案

第一个薄g我会考虑更好地规范化您的数据库模式;如果您的模型的单个实例可以具有此字段的多个值,该字段可能应该是与ForeignKey的链接模型。



如果您使用Postgres,您也可以使用ARRAY字段; Django现在已经内置的支持



如果你不能做任何一个,那么你基本上需要重新实现一个(更好的)版本的CommaSeparatedIntegerField。原因是CommaSeparatedIntegerField只是一个纯粹的CharField,其默认格式表示是正则表达式验证的文本输入。换句话说,它不会对您有用的任何事情。



您需要编写的是一个自定义的ListField或MultipleValuesField,它们期待Python列表并返回一个Python列表,但内部将该列表转换为/从逗号分隔的字符串插入数据库。请阅读有关自定义模型字段的文档;我想在你的情况下,你会想要一个CharField的子类,覆盖了两种方法: to_python (将CSV字符串转换为Python列表)和 get_db_prep_value (将Python列表转换为CSV字符串)。


Say I have choices defined as follows:

choices = (('1','a'),
           ('2','b'),
           ('3','c'))

And a form that renders and inputs these values in a MultipleChoiceField,

class Form1(forms.Form):
    field = forms.MultipleChoiceField(choices=choices)

What is the right way to store field in a model.

I can of course loop through the forms.cleaned_data['field'] and obtain a value that fits in models.CommaSeperatedIntegerField.

Again, each time I retrieve these values, I will have to loop and convert into options.

I think there is a better way to do so, as in this way, I am in a way re-implementing the function that CommaSeperateIntegerField is supposed to do.

解决方案

The first thing I would consider is better normalization of your database schema; if a single instance of your model can have multiple values for this field, the field perhaps should be a linked model with a ForeignKey instead.

If you're using Postgres, you could also use an ARRAY field; Django now has built-in support.

If you can't do either of those, then you do basically need to reimplement a (better) version of CommaSeparatedIntegerField. The reason is that CommaSeparatedIntegerField is nothing but a plain CharField whose default formfield representation is a regex-validated text input. In other words, it doesn't do anything that's useful to you.

What you need to write is a custom ListField or MultipleValuesField that expects a Python list and returns a Python list, but internally converts that list to/from a comma-separated string for insertion in the database. Read the documentation on custom model fields; I think in your case you'll want a subclass of CharField with two methods overridden: to_python (convert CSV string to Python list) and get_db_prep_value (convert Python list to CSV string).

这篇关于直接在模型中存储django forms.MultipleChoiceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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