Django - 使用Formsets在没有通过表的情况下建立2种模型之间的多对多关系

时间:2012-12-08 06:50:28

标签: python django forms

我有一个模型属性和产品,声明如下:

class Attribute(models.Model):
    value = models.TextField()
    owner = models.ForeignKey(User)
    type = models.ForeignKey(AttributeType)     
    image = ImageField(upload_to='attributes', null=True, blank=True)     
    related_attribute = models.ManyToManyField('self', blank = True, null = True) 

class BaseWorkspace(models.Model):
    name = models.CharField(max_length=255)
    owner = models.ForeignKey(User)
    attributes = models.ManyToManyField('Attribute', blank = True, null = True)
    created = CreationDateTimeField()
    modified = ModificationDateTimeField()
    comments = models.ManyToManyField('Comment', blank = True, null = True )
    sort_order = models.IntegerField(blank = True)

class Product(BaseWorkspace):
    project = models.ForeignKey('Project', related_name='products')

如何使用formset建立m-m关系?我尝试过像这样的模型formset工厂:

AttributeFormset = modelformset_factory(Attribute, form=AttributeForm)

在通用视图中使用此函数:

def form_valid(self, form):
        f = form.instance
        f.sort_order = Product.default_sort_order()
        f.owner = self.request.user
        f.project = get_object_or_404(Project, pk=self.kwargs['pk'])
        context = self.get_context_data()
        attribute_form = context['attribute_form']
        if attribute_form.is_valid():
            self.object = form.save()
            attribute_form.instance = self.object
            attribute_form.save()
            return HttpResponseRedirect(reverse(self.get_success_url()))
        else:
            return self.render_to_response(self.get_context_data(form=form))

但我无法让它发挥作用。任何想法?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

from django.forms.models import modelformset_factory
def my_view_function(request) :

    # not sure where the product whose formset we are working on comes from
    product = <whatever>

    AttributeFormSet = modelformset_factory(Attribute)

    if request.method == "POST" :
        # POST bound formset
        formset = AttributeFormSet(request.POST, queryset=Attribute.objects.filter(product=product))
        # If the entire formset is valid
        if formset.is_valid() :
            for form in formset:
                # Save each form in the set
                b = form.save()
        else : 
            #There was an error (add a message using the messages framework?)
            pass
    else :
        # initial formset w/o post
        formset = AttributeFormSet(queryset=Attribute.objects.filter(product=product))

    ...

很难给你更具体的答案,如果你使用的是基于类的视图,我认为我们需要整个视图函数或视图类。

在你的模板中,一些简单的东西(来自文档)应该这样做。

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>

如果您需要能够在运行时使用javascript将表单添加到formset,请查看:http://code.google.com/p/django-dynamic-formset/。我从未使用它,但至少看起来它是朝着正确方向迈出的一步。

修改

首先从formset中排除产品

AttributeFormSet = modelformset_factory(Attribute, exclude=('product',))

然后将表单处理块更改为不提交保存,并手动附加产品。

        if formset.is_valid() :
            for form in formset:
                # get this form's instance
                b = form.save(commit=False)
                # attach product
                b.product = product
                # save the instance
                b.save()

答案 1 :(得分:0)

使用f = form.instance访问原始实例。如果attribute_form有效,则调用form上的保存方法,而不是f。您在f上所做的所有更改都将丢失。

在保存之前,请查看saving-objects-in-the-formset如何更新formset的实例。