django manytomany字段使用通过和formwizard

时间:2014-01-16 03:16:56

标签: django django-forms django-views django-formwizard

我正在尝试创建一个非常复杂的表单并使用formwizard将其分解。我要做的第一件事就是让ManyToManyField使用直到显示,然后我需要弄清楚如何使它全部保存。

#models.py
----------------------

class Meat(models.Model):
    name = models.charField(max_length=200)
    company = models.CharField(max_length = 200)

class Starch(models.Model):
    name = models.CharField(max_length=200)
    company = models.CharField(max_length=200)



class Recipe(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField(help_text='Please describe the finished dish')
    meat = models.ManyToManyField('Meat' through='RecipeMeat')
    meat_notes = models.TextField()
    starch = models.ManyToManyField('Starch' through='RecipeStarch')
    starch_notes = models.TextField()



class RecipeMeat(models.Model):
    recipe = models.ForeignKey(Recipe)
    meat = models.ForeignKey(Meat)
    qty = models.FloatField()

class RecipeStarch
    recipe = models.ForeignKey(Recipe)
    starch = models.ForeignKey(Starch)
    qty = models.FloatField()

#forms.py
-------------------

class RecipeForm(forms.ModelForm):
    class Meta:
        model = Recipe
        fields = ('name', 'description')


class RecipeMeatForm(forms.ModelForm):
    class Meta:
        model = RecipeMeat

class RecipeMeatNotesForm(forms.ModelForm):
    class Meta:
        model = Recipe
        fields = ('meat_notes',)

class RecipeStarch(forms.ModelForm):
    class Meta:
        model = RecipeStarch

class RecipeStarchNotesForm(forms.ModelForm):
    class Meta:
        model = Recipe
        fields = ('starch_notes')

MeatFormSet = inlineformset_factory(Recipe, RecipeMeat, form=RecipeMeatForm, extra=1)

#views.py
---------------------------


class CreateRecipeWizard(SessionWizardView):
    template_name = "create-recipe.html"
    instance =  None
    file_storage = FileSystemStorage(location= 'images')

    def dispatch(self, request, *args, **kwargs):
        self.instance = Recipe()
        return super(CreateRecipeWizard, self).dispatch(request, *args, **kwargs)

    def get_form_instance( self, step ):
        return self.instance

    def done( self, form_list, **kwargs ):
         self.instance.save()
        return HttpResponseRedirect(reverse(all-recipes))

#urls.py
------------------------------

 url(r'^create-recipe/$', views.CreateRecipeWizard.as_view([RecipeForm, MeatFormSet, RecipeMeatNotesForm, RecipeStarchNotesForm]), name='create-recipe'),

我有点像这个django东西的菜鸟。食谱部分更长,更复杂,但几乎相同的模式。如果任何人可以帮助指出我如何让​​我的ManyToManyField使用通过部分找出或指向正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

要在formwizard进程中保存ManyToMany关系,您可以执行以下操作;

def done(self, form_list, **kwargs):
    form_data_dict = self.get_all_cleaned_data()
    m2mfield = form_data_dict.pop('m2mfield')

    instance = form_list[0].save()
    for something in m2mfield:
        instance.m2mfield.add(something)

    return render_to_response(
        'done.html', {},
        context_instance=RequestContext(self.request)
    )

在此示例中,列表中的第一个表单是ModelForm,用于我尝试创建的内容,并且它有一个ManyToManyField到另一个模型,我有一个表单第二个这个过程。所以我抓住了第一个形式&保存它,然后从第二个表单中的清理数据中获取字段,并将选定的选项保存到M2M字段。