2011-11-09 11 views
0

私は、次のモデルがあります:ジャンゴ選択ウィジェットグループテンプレート

# 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] [+] [-] 

開始時に、あなたが常に2つの選択グループに1つ、キー/値のペアのための1つを取得します。 GROUPで+キーを押すと、新しいグループ選択ボタンがキー/値ペア選択と共に表示されます。キー/値で+キーを押すと、新しいキー/値選択ボックスが表示されます。

私は2つの問題を抱えて:

ONE:私はテンプレートでのチェックがどのように見えると TWOすべきかわからない:私はそれらの+を実装するにはどうすればよい - 任意のヘルプは高く評価されボタン

。これはjavascriptがなければ可能だろうがクールですが、私はその方向に非常に高い希望がありません

+1

あなたのモデルにある 'Admin'クラスは何ですか? –

+0

私の質問には関係なく、すでに削除されています –

+0

単純にCSSを使用してjavascriptなしでも可能ですが、djangoに投稿アクションを返して、いくつのグループやキーウィジェットにパラメータを付けてフォームを再レンダリングする必要があります表示して保存する必要があります。 Javascriptははるかにクリーンなソリューションです。 – astevanovic

答えて

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から継承し、その__init__(request, descriptor)メンバにmake_groupkeys_form(記述子)コードを入れるように書き換えます。選択フィールドを列挙するには、自分でis_valid()と書く必要があります。また、ユーザーがフォームを送信したときに選択肢が正しいことを確認し、個々のユーザーの選択を検証するためにclean()を上書きする必要があります。

最後に、このdynamic forms readを通過することを検討してください。 djangoで動的フォームを作成する方法を段階的に紹介します。

関連する問題