我有一个模型属性和产品,声明如下:
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))
但我无法让它发挥作用。任何想法?
答案 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的实例。