2014-01-16 3 views
2

꽤 복잡한 폼을 만들고 formwizard를 사용하여 해체하려고합니다. 내가하려고하는 첫 번째 일은 표시 할 ThroughTo를 사용하는 ManyToManyField를 얻는 것입니다. 그러면 모든 것을 저장하는 방법을 알아야합니다.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'), 

.

저는이 장고에 관한 약간의 신인입니다. 레서피 부분은 훨씬 길고 복잡하지만 거의 같은 패턴입니다. 어떤 사람이 올바른 부분에 대해 알아 보거나 지적한 부분을 사용하여 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 필드에 저장합니다.