Django选择小组件组模板

时间:2011-11-09 09:18:28

标签: django forms templates

我有以下型号:

# 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选择,如果按Key / Value选择+,则会出现一个新的键/值选择框。

我有两个问题:

ONE:我不知道模板中的检查应该如何 TWO:我如何实现这些+ - 按钮

感谢任何帮助。如果没有javascript可以实现这一点会很酷,但我对这个方向没有太大的希望

1 个答案:

答案 0 :(得分:0)

您需要动态构建表单。假设您从视图处理程序中的视图(包含组模型外键和其中的实际键数)中定义了在字典中构建的组表单:

# 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中创建动态表单。