2012-03-22 17 views
1

私は、1つまたは 個の属性で構成されたたくさんのアイテムを含むサンドボックスを持っています。各属性は、特定の属性タイプ(色、形状など)に属します。 AttributeTypesとともにグループ化されたAttributesを使用してフォームをレンダリングする方法が失われています。多くの種類から多くの選択肢をタイプ別にグルーピングする

モデル

class Sandbox(models.Model): 
    item = models.ForeignKey('Item') 

class Item(models.Model): 
    name = models.CharField() 
    sandbox = models.ForeignKey(Sandbox) 
    attributes = models.ManyToManyField('Attribute') 

class Attribute(models.Model): 
    name = models.CharField() 
    type = models.ForeignKey('AttributeType') 

class AttributeType(models.Model): 
    name = models.CharField() 

class ItemAttribute(models.Model): 
    # intermediary model to hold references 
    item = models.ForeignKey(Item) 
    type = models.ForeignKey(AttributeType) 
    attribute = models.ForeignKey(Attribute) 

のModelForm

class Sandbox(ModelForm): 
    class Meta: 
     model = Sandbox 

のみ属性の型ごとに選択肢が存在する場合があります。たとえば、あるものは1つの色 または1つの形状しか持つことができません。

AttributeType Attribute  Users choice 
    color 
        red 
        blue   [x] 
        green 
        shape 

    shape 
        triangular 
        squared   [x] 
        spherical 

これは固まっていました。これらの属性をフォーム内でグループ化するにはどうすればできますか。ラジオボタンを使用してタイプごとに1つの属性のみを選択するにはどうすればよいですか? 単純なモデル表現を使用していた私の元の考えは、おそらくここでは不足していますか? 私はドキュメント、StackOverflow、Googleを試しましたが、運はありません。

ヒントとアイデアを歓迎します。

私のソリューション

私は私のニーズを満たすソリューションを構築。 @bmihelacは、カスタムフォームを作成するためのファクトリメソッドを作成する方法について、この記事の良い方向を指摘しました。私は、このファクトリメソッドを呼び出して、フォームを起動します

def make_sandbox_form(item): 

    def get_attributes(item): 
     item_attributes = ItemAttribute.objects.filter(item=item) 
     # we want the first attribute type 
     _attr_type = item_attributes[0].attribute.all()[0].attribute_type 

     choices = []  # holds the final choices 
     attr_fields = {} # to hold the final list of fields 
     for item_attrs in item_attributes.all(): 
      attributes = item_attrs.attribute.all().order_by('attribute_type') 
      for attr in attributes: 
       print attr.attribute_type, ' - ' , _attr_type 
       if attr.attribute_type == _attr_type: 
        choices.append((attr.pk, attr.value)) 
       else: 
        d = {u'%s' % _attr_type : fields.ChoiceField(choices=choices, widget=RadioSelect)} 
        attr_fields = dict(attr_fields.items() + d.items()) 
        # set the _attr_type to new type and start over with next attribute type 
        _attr_type = attr.attribute_type 
        choices = [] 

     return attr_fields 

    form_fields = { 
     'item' : fields.IntegerField(widget=HiddenInput), 
    } 
    form_fields = dict(form_fields.items() + get_attributes(item).items()) 

    return type('SandboxForm', (forms.BaseForm,), { 'base_fields' : form_fields}) 

[下記参照]:= make_sandbox_form フォーム()

http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/

(ウィッシュ私はすべてが、私は欠けているStackOverflowの新人であることをupvoteことができ

+1

あなたは[ 'regroup'](https://docs.djangoproject.com/en/dev/ref/templates/builtins/で見たことがありますか? from = olddocs#regroup)テンプレートタグ(属性でグループ化できるようにする) –

+0

はい、私はフォームを使って実装する方法を見つけることができません。私は考えているのは、この問題は新しいフォームのレンダリング方法を作ることで解決できるということです...質問はどうですか... – droidballoon

答えて

1

を私はすべてのAttributeTypeでのために1つの選択フィールドを作成し、ダイナミックなフォームを作成します。

次に、ウィジェットをラジオボタンで簡単に置き換えることができます。

この記事は参考になり得る:

http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/

+0

面白い、ありがとう!私はリンクを調べます。 – droidballoon

1

1つのサンドボックスに複数のアイテムがあることができますか、1つのアイテムに複数のサンドボックスがありますか?一度に複数のサンドボックスにアイテムを含めることはできますか?

私はあなたが1つのサンドボックスは、多くの項目を含むようにしたいと思う:

class Sandbox(models.Model): 
    name = models.CharField() 

class Item(models.Model): 
    name = models.CharField() 
    sandbox= models.ForeignKey(Sandbox) 
    attributes = models.ManyToManyField('Attribute') 

同じ分析はここに当てはまり:

は一つの属性は、多くの属性タイプを持っていない、または1つの属性タイプは、多くの属性を持っていますか?

ここでは、関係権利を持って、私はちょうどので、各項目が属性を持っており、彼らは常に、これらの属性、色や形に恵まれているモデルの順

class AttributeType(models.Model): 
    name = models.CharField() 

class Attribute(models.Model): 
    name = models.CharField() 
    type = models.ForeignKey(AttributeType) 

を切り替えます。

あなたはのように見えたデータでしたテーブル可能性がありますが:私は論理的に同じであるデータが一緒にグループ化する必要があると思うので、私は個人的にそれをしないだろう

pk type 
1 green 
2 circular 
etc 

を。図形は色とは異なる属性を持っています。たとえば、私の要点を説明するために、色のRGBを望むならどうしますか?次に、必要がなく、混乱している場合には、図形用に余分な列があります。逆にも同じことですが、色には次元がありません。 ForeignKeyのためChioceField(IIRC)にdjango.Formsのデフォルトは、あなたが、これはまた、あなたが唯一ごとに選択肢を持って保証

class Color(models.Mode): 
    #info about colors 

class Shape(models.Mode): 
    #info about shapes 

class Item(models.Model): 
    name = models.CharField() 
    sandbox= models.ForeignKey(Sandbox) 
    color= models.ForeignKey(Color) 
    shape= models.ForeignKey(Shape) 

、:

代わりに、私は次のように何かをするかもしれません。それをオーバーライドし、そのラジオボタン作るためとして

、ちょうどここにドキュメントを確認してください。

https://docs.djangoproject.com/en/dev/ref/forms/widgets/

+0

確かに、あなたの鋭い目に気付きました。私の質問を更新しました。ありがとう!重要な点は、この場合は管理インターフェースからユーザーを追加するため、それぞれのタイプごとに個別のモデルを作成できないということです。 AttributeTypesの数は私の初期の考え方を超えて拡大することができます。 – droidballoon

+1

@droidballoonでも、それらを分離してデータ入力をユーザにプッシュすることができます。色が存在しない場合は、新しい色や形を作成するオプションを与えます。 –

+0

実際、私が意図したことは、ユーザーが新しいAttributeTypeを作成したい場合、「Sound」と言うと、開発者はこのAttributeTypeの新しいモデルを作成して変更に対応する必要があるということでした。または私は何かを逃している? – droidballoon

関連する問題