(
('one', 'One'),
('two', 'Two'),
('other', EDITABLE_HUMAN_READABLE_CHOICE),
)
So what I would like is a choices field with some common choices that are used frequently, but still be able to have the option of filling in a custom human readable value.
因此,我想要的是一个选择字段,其中有一些常用的选择,但仍然能够填充自定义的人类可读值。
Is this possible or is there some better way of doing this that I am completely missing?
这是可能的还是有更好的方法来实现我完全没有的?
2 个解决方案
#1
6
One way to do this would be to use a custom ModelForm
for admin. This form can have two fields - one that accepts a set of predefined choices and another one that accepts arbitrary values. In the clean()
method you can ensure that only one of these has been selected.
一种方法是为admin使用自定义的ModelForm。该表单可以有两个字段—一个接受一组预定义的选项,另一个接受任意值。在clean()方法中,您可以确保只选择了其中一个。
If you are particular about how the UI should look - say, radio buttons that allow you to choose either a pre-defined value xor enter a custom value, then you may have to come up with your own custom field.
如果您特别关注UI的外观——比如允许您选择预定义值xor或输入自定义值的单选按钮,那么您可能需要创建自己的自定义字段。
#2
3
I quick solution I used:
我使用的快速解决方案:
- used standard ModelChoice Fields
- 使用标准ModelChoice字段
- custom Form, which added a regular Input Field for every ModelChoice Field
- 自定义表单,它为每个ModelChoice字段添加了一个常规输入字段
- custom JQuery which showed the regular Input Field only, when the last choice is selected
- 自定义JQuery仅在选择最后一个选项时才显示常规输入字段。
- in the View, when I got POST data I parsed the POST array and created the Models
- 在视图中,当我得到POST数据时,我解析了POST数组并创建了模型
#1
6
One way to do this would be to use a custom ModelForm
for admin. This form can have two fields - one that accepts a set of predefined choices and another one that accepts arbitrary values. In the clean()
method you can ensure that only one of these has been selected.
一种方法是为admin使用自定义的ModelForm。该表单可以有两个字段—一个接受一组预定义的选项,另一个接受任意值。在clean()方法中,您可以确保只选择了其中一个。
If you are particular about how the UI should look - say, radio buttons that allow you to choose either a pre-defined value xor enter a custom value, then you may have to come up with your own custom field.
如果您特别关注UI的外观——比如允许您选择预定义值xor或输入自定义值的单选按钮,那么您可能需要创建自己的自定义字段。
#2
3
I quick solution I used:
我使用的快速解决方案:
- used standard ModelChoice Fields
- 使用标准ModelChoice字段
- custom Form, which added a regular Input Field for every ModelChoice Field
- 自定义表单,它为每个ModelChoice字段添加了一个常规输入字段
- custom JQuery which showed the regular Input Field only, when the last choice is selected
- 自定义JQuery仅在选择最后一个选项时才显示常规输入字段。
- in the View, when I got POST data I parsed the POST array and created the Models
- 在视图中,当我得到POST数据时,我解析了POST数组并创建了模型