开发者

How do you make a choices field in django with an editable "other" option?

(
    ('one', 'One'),
    ('two', 'Two'),
    ('other', EDITABLE_HUMAN_READABLE_CHOICE),
)

So what I would like is a choice开发者_开发百科s 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?


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.

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.


I quick solution I used:

  • used standard ModelChoice Fields
  • custom Form, which added a regular Input Field for every ModelChoice Field
  • custom JQuery which showed the regular Input Field only, when the last choice is selected
  • in the View, when I got POST data I parsed the POST array and created the Models
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜