2013-05-06 56 views
1

所以,这里是我的问题:我试图生成一大堆特性的一个大表格,其中一些如果他们是布尔值,一些用文本填充,一些用JS微调器增加或减少。但是,这里是捕获,所有这些都是用户生成的。我所做的是:在某些时候,我邀请用户通过向模型Option添加对象来创建它的特性,其中他告诉我特性的名称和它的类型(布尔,文本或数字)。使用Django Formset从多个模型生成动态表单

这里是我的简化模型:

class Characteristic(models.Model): 
     option = models.ForeignKey('Option') 

    class Option(models.Model): 
     option_name = models.CharField(max_length=100) 
     OPTION_TYPE_CHOICES = (
      ('B',u'Bolean'), 
      ('T',u'Textual'), 
      ('N',u'Numerical'), 
    ) 
     option_type = models.CharField(max_length=1,choices=OPTION_TYPE_CHOICES) 

    class Boolean(Characteristic): 
     value = models.BooleanField() 

    class Textual(Characteristic): 
     value = models.CharField(max_length=100) 

    class Numerical(Characteristic): 
     value = models.PositiveIntegerField() 

到目前为止,一切都很好。

当生成具有所有可能特征的表单时,问题就开始了。我所做的是这样的:

这是我的观点:

@login_required 
def AddCharacteristic(request): 

    numerical_options = Option.objects.filter(option_type='N') 
    textual_options = Option.objects.filter(option_type='T') 
    boolean_options = Option.objects.filter(option_type='B') 

    NumericalFormset = modelformset_factory(Numerical,extra=len(numerical_options)) 
    TextualFormset = modelformset_factory(Textual,extra=len(textual_options)) 
    BooleanFormset = modelformset_factory(Boolean,extra=len(boolean_options),form=forms.BooleanForm) 

    if request.method == 'POST': 
     numerical_formset = NumericalFormset(request.POST,prefix='numerical')· 
     textual_formset = TextualFormset(request.POST,prefix='textual') 
     boolean_formset = BooleanFormset(request.POST,prefix='boolean') 
     if numerical_formset.is_valid() and textual_formset.is_valid() and boolean_formset.is_valid(): 
      numerical_formset.save() 
      textual_formset.save() 
      boolean_formset.save() 
     if 'next' in request.GET: 
      return HttpResponseRedirect(request.GET['next']) 
     else: 
      return HttpResponseRedirect('../../list/') 
    else: 
     boolean_formset = BooleanFormset(queryset=Boolean.objects.none(),prefix='boolean',initial=[{'option':n.id} for n in boolean_options]) 
     textual_formset = TextualFormset(queryset=Textual.objects.none(),prefix='textual',initial=[{'option':n.id} for n in textual_options]) 
     numerical_formset = NumericalFormset(queryset=Numerical.objects.none(),prefix='numerical',initial=[{'option':n.id} for n in numerical_options]) 
    return render_to_response('characteristics/add.html', { 
     'numerical_formset': numerical_formset, 
     'textual_formset': textual_formset, 
     'boolean_formset': boolean_formset, 
    },context_instance=RequestContext(request)) 

在我看来:

<form method="post"> 

    {% csrf_token %} 

    {{ numerical_formset.management_form }} 
    {% for numerical_form in numerical_formset %} 
    {{ numerical_form }}<br> 
    {% endfor %} 

    {{ boolean_formset.management_form }} 
    {% for boolean_form in boolean_formset %} 
    {{ boolean_form }}<br> 
    {% endfor %} 

    {{ textual_formset.management_form }} 
    {% for textual_form in textual_formset %} 
    {{ textual_form }}<br> 
    {% endfor %} 
    <button type="submit">Save</button> 
    <button type="reset">Cancel</button> 
</form> 

因此,它是一种工作,但主要的问题至今:

  • 我无法重写小部件,将特征选项字段从下拉列表转换为标签。
  • 确认工作。如果用户在数字字段中或其他方式放置文本,Django它不抱怨。
  • 一般的感觉,我搞砸了完全和工程的东西。 :)

回答

1

它看起来像你试图创建自己的实现EAV。你可能想用django-eav代替。

如果你仍然想要自己的方式,你可以使用​​作为参考。

此外,Django的主要开发人员之一Jacob Kaplan-Moss撰写了一篇名为“Dynamic form generation”的文章(文章发表于2010年,但截至今日,所述解决方案仍在工作) 。

还有其他方法来存储和检索动态数据(如django-hstoredjango-mutant)。我已经written关于他们before

+0

作为旁注:还有一个名为'django-hstore-flattenfields'的应用程序,它使用[hack](https://github.com/multmeio/django-hstore-flattenfields/blob/master/hstore_flattenfields/models .py)使Django [生成](https://github.com/multmeio/django-hstore-flattenfields/blob/master/hstore_flattenfields/forms.py)动态表单,就好像它们是'ModelForm'一样。 – 2013-05-06 15:34:53