2014-01-16 56 views
2

我想创建一个非常复杂的形式并使用formwizard打破它。我想要做的第一件事就是让ManyToManyField使用through来显示,然后我需要弄清楚如何使它全部保存。django manytomany字段使用通过和formwizard

#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使用通过部分想出或指出正确的方向,将不胜感激。

回答

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字段。