django manytomany字段使用through和formwizard [英] django manytomany field using through and formwizard

查看:55
本文介绍了django manytomany字段使用through和formwizard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常复杂的表单,并使用formwizard对其进行分解.我想做的第一件事是通过显示要使用的ManyToManyField,然后我需要弄清楚如何保存所有内容.

I am trying to create a pretty complicated form and break it up using formwizard. The first thing I am trying to do is get the ManyToManyField using through to display, Then I need to figure out how to make it all save.

#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或指出正确的方向,那将不胜感激.

I am a bit of a rookie with this django stuff. The Recipe part is much longer and more complicated but pretty much the same pattern. If any one could help point me in the right on how to get my ManyToManyField using through part figured out or pointed in the right direction it would be greatly appreciated.

推荐答案

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

To save the ManyToMany relationship on a formwizard process you can do something like this;

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字段中.

In this example the first form in the list is a ModelForm for the thing I'm trying to create and it has a ManyToManyField to another model for which I have a form second in the process. So I grab that first form & save it, then grab the field from the cleaned data from the second form and save the selected options to the M2M field.

这篇关于django manytomany字段使用through和formwizard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆