Django的选择控件组模板
问题描述:
我有以下型号:Django的选择控件组模板
# Group for Key/Value pairs
class Group(models.Model):
name = models.TextField(unique=True)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Group'
verbose_name_plural = 'Groups'
# Key is the key/name for a Value
class Key(models.Model):
name = models.TextField(unique=True)
def __unicode__(self):
return self.name
class Meta:
verbose_name = 'Key'
verbose_name_plural = 'Keys'
# Value is the value/content of a key
class Value(models.Model):
value = models.TextField()
def __unicode__(self):
return '%s' % self.value
class Meta:
verbose_name = 'Value'
verbose_name_plural = 'Values'
class Key_Value(models.Model):
group = models.ForeignKey(Group)
key = models.ForeignKey(Key)
value = models.ForeignKey(Value)
def __unicode__(self):
return '%s = %s' % (self.key.name, self.value.value)
class Meta:
verbose_name = 'Key/Value Paar'
verbose_name_plural = 'Key/Value Paare'
Now I pass the form to the template:
def exampleview(request):
key_value_form = Key_Value_Form(request.POST)
return render_to_response(
'edit.html', {
'key_value_form': key_value_form,
})
现在让我们来看看可能的数据
KEY/VALUE PARIRS:
key = TEST 1
value = TEST 1
group = TESTGROUP 1
key = TEST 2
value = TEST 2
group = TESTGROUP 2
现在我改变了默认的小部件键/值表条目选择小部件。
这就是我想做的事:
SELECT GROUP [+] [-]
--> [Now choose Key/Value pair belonging to group] [+] [-]
在一开始,你总是得到两个选择一个组,一个是键/值对。 如果在GROUP中按+,则新的组选择按钮应与KEY/Value Pair选择一起出现,如果按键/值处的+选择应出现新的键/值选择框。
我有两个问题:
ONE:我不知道在模板中的检查应该怎么看起来和 二:我如何能实现那些+ - 按键
任何帮助表示赞赏。这将是很酷,如果这可能没有javascript,但我没有很高的期望在这个方向
答
你需要建立在飞行中的形式。假设你有内置在字典中从视图在视图处理程序(含模型外键和实际键的数量在其中)一组形式的定义:
# note that the dictionary d shown here is just an example
# yours should have actual group foreign keys based on Group model
d = { 'first' : 5, 'second' : 3, 'third' : 2 }
def make_groupkeys_form(descriptor):
groups = { 'select': '', 'keypairs' : [] }
fields = { 'groups' : groups }
for group, numkeys in descriptor.values():
groupchoices = # find out what choices you need to offer for group widget
groups['select'] = forms.SelectField(choices=groupchoices)
for i in range(numkeys):
keyvalchoices = # find out what choices you need to offer for keyval widget per group
groups['keypairs'].append(forms.SelectField(choices=keyvalchoices))
# now you have enough SelectFields for all your groups and keyval pairs
return type('Key_Value_Form', (forms.BaseForm,), { 'base_fields': fields })
# from view handler
def exampleview(request):
# determine from request how many groups you want and how many keyval widgets per group, for example here I will use the above predefined d
key_value_form = make_groupkeys_form(d)
return render_to_response(
'edit.html', {
'key_value_form': key_value_form,
})
注意,你可以(也应该)将类Key_Value_Form
重写为从forms.BaseForm继承,并将make_groupkeys_form(描述符)代码放入其__init__(request, descriptor)
成员中。您还需要编写自己的is_valid()
以通过选择字段进行枚举,并确保在用户提交表单时覆盖选项并覆盖clean()
以尝试验证个人用户的选择。
最后,考虑通过这个dynamic forms read。它将逐步引导您如何在django中创建动态表单。
你模型中的那些'Admin'类是什么? –
与我的问题没有关系,它们已经被删除 –
没有javascript可以使用简单的CSS,但是你必须将提交后的动作提交给django,使用一个参数重新渲染表单,其中包含多少个组和/或关键小部件需要显示和保存。尽管JavaScript是更清洁的解决方案。 – astevanovic