Django, create multiselect checkbox input

我的未来我决定 提交于 2020-01-06 15:18:14

问题


I have a model with an integer field and would like to have an alternative form input render other than the default textbox, so that I have the following:

models.py: myfield = models.IntegerField(default=7, blank=True)

Would like to have the following:

[x] Choice A (value 0)

[ ] Choice B (value 1)

[x] Choice C (value 2)

So that upon save the choices would be calculated like the the following: (since choice A and C are selected and 2 is not selected).

myfield = sum(choice selected multiplied by the 2 to the value of the selected choice)

so that:

my field = (1 * 2^0) + (0 * 2^1) + (1 * 2^2)

my field = 1 + 0 + 4

my field = 5 #final value

If this was anything outside of Django, it would be more straight-forward via standard Checkbox grouping that does not map directly to a database field in the model, but with Django, i'm currently at a loss.

I have referenced the following materials, but still am unable to figure out what to do:

widgets

modelforms

Thanks for your help and input.


回答1:


You can use custom widget like this:

from django import forms

class BinaryWidget(forms.CheckboxSelectMultiple):
    def value_from_datadict(self, data, files, name):
        value = super(BinaryWidget, self).value_from_datadict(data, files, name)
        if name == 'myfield':
            value = sum([2**(int(x)-1) for x in value])
        return value

and override myfield in your modelform as follows:

class MyModelForm(forms.ModelForm):
    ...
    myfield = forms.IntegerField(widget=BinaryWidget(choices=(
        ('1', '2^0',), ('2', '2^1',), ('3', '2^2',)
        )))

this will give you the expected result, although you probably will need a reverse feature - set initial values when editing for example, if so please let me know, so we can figure editing as well.




回答2:


Try Select2. It's a much better option.



来源:https://stackoverflow.com/questions/19645227/django-create-multiselect-checkbox-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!