如何禁用MultipleChoiceField中的复选框? [英] How to disable checkboxes in MultipleChoiceField?
本文介绍了如何禁用MultipleChoiceField中的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表单中使用 MultipleChoiceField 。它显示了带有复选框的 REQUIREMENTS_CHOICES 列表,用户可以在其中选择并向数据库添加新需求。是否可以禁用数据库中已经存在的复选框(就我而言)?可以说我的数据库中有A和C,所以我不需要它们。
I use MultipleChoiceField in form. It shows me REQUIREMENTS_CHOICES list with checkboxes where user can select and add new requirements to database. Is it possible to disable checkboxes (in my case requirements) which is already in the database? Lets say I have A and C in database so I dont need them.
tulpe中的第一个值是需求的象征,第二个值是需求的名称。
First value in tulpe is symbol of requirement, second value is the name of requirement.
models.py:
class Requirement(models.Model):
code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
symbol = models.CharField(_('Symbol'), max_length=250)
name = models.CharField(_('Name'), max_length=250)
forms.py:
REQUIREMENTS_CHOICES = (
('A', 'Name A'),
('B', 'Name B'),
('C', 'Name C'),
('D', 'Name D'),
)
class RequirementAddForm(forms.ModelForm):
symbol = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=REQUIREMENTS_CHOICES,)
class Meta:
model = Requirement
fields = ('symbol',)
views.py:
def requirement_add(request):
data = dict()
if request.method == 'POST':
form = RequirementAddForm(request.POST)
if form.is_valid():
list = dict(REQUIREMENTS_CHOICES) # {'C': 'Name C', 'A': 'Name A', 'B': 'Name B'}
symbols = form.cleaned_data.get('symbol') # ['A', 'B', 'C']
requirement = form.save(commit=False)
for symbol in symbols:
requirement.symbol = symbol
requirement.name = list[symbol]
requirement.save()
data['form_is_valid'] = True
requirements = Requirement.objects.filter()
context = {requirement': requirement, 'requirements': requirements}
data['html_requirement'] = render_to_string('project/requirement_list.html', context)
else:
data['form_is_valid'] = False
else:
form = RequirementAddForm()
context = {'form': form}
data['html_requirement_form'] = render_to_string('project/requirement_add.html', context, request=request)
return JsonResponse(data)
推荐答案
您可以在表单初始化时对其进行操作:
You can manipulate your form at it's initialization:
REQUIREMENTS_CHOICES = (
('A', 'Name A'),
('B', 'Name B'),
('C', 'Name C'),
('D', 'Name D'),
)
class RequirementAddForm(forms.ModelForm):
def __init__(self, symbols='', *args, **kwargs):
super(RequirementAddForm, self).__init__(*args, **kwargs)
UPDATED_CHOICES = () # empty tuple
for choice in REQUIREMENTS_CHOICES:
if choice[1] not in symbols:
UPDATED_CHOICES += (choice,) # adds choice as a tuple
self.fields['symbol'] = forms.MultipleChoiceField(
required=False,
widget=forms.CheckboxSelectMultiple,
choices=UPDATED_CHOICES,
)
class Meta:
model = Requirement
上面发生了什么:
- 初始化表单时
(例如:form = RequirementAddForm(symbols =存在的符号)
),您可以将包含数据库中某个项目的现有符号的字符串传递给构造函数。 -
__ init__
函数检查符号
中已经存在的选择,并相应地更新UPDATED_CHOICES
。 - 从构造函数中将一个名为
symbol
的字段添加到表单中,该字段可以选择UPDATED_CHOICES
。
- When you initialize your form
(ex:form=RequirementAddForm(symbols=existing_symbols)
), you can pass to the constructor, a string with the existing symbols for an item in your database. - The
__init__
function checks which choices exist already insymbols
and updates theUPDATED_CHOICES
accordingly. - A field named
symbol
gets added to the form from the constructor, which have as choices theUPDATED_CHOICES
.
这篇关于如何禁用MultipleChoiceField中的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文