2014-01-16 40 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字段。