我读了很多关于这个问题的链接:
包括DOC: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships
但是不能通过中间课程来保存我的M2M。
我的模特:
class Promotor(PessoaFisica):
user = models.OneToOneField(User, blank=True, null=True)
class Setor(models.Model):
nome = models.CharField(max_length=255)
promotores = models.ManyToManyField(Promotor, through='Membro', blank=True, null=True)
class Meta:
verbose_name_plural = 'setores'
def __unicode__(self):
return "%s" % (self.nome)
class Membro(models.Model):
promotor = models.ForeignKey(Promotor)
setor = models.ForeignKey(Setor)
data_inclusao = models.DateField(auto_now=True)
保存方法:
def add_setor(request):
form = SetorForm(request.POST or None)
if form.is_valid():
s = form.save()
setor = get_object_or_404(Setor, pk = s.id)
for promotor_id in request.POST.getlist('promotores'):
membro = Membro.objects.create(promotor_id=promotor_id, setor_id=setor.id)
membro.save()
messages.add_message(request, messages.SUCCESS, 'Setor cadastrado com sucesso!')
return HttpResponseRedirect('/project/setor/index/')
return render_to_response('project/setor/form.html', locals(), context_instance=RequestContext(request))
错误是:
Cannot set values on a ManyToManyField which specifies an intermediary model. Use setor.Membro's Manager instead.
如果save()方法有提交" false"," setor"没有" id"放入Membro对象。 如何通过中间类保存setor? 谢谢!
答案 0 :(得分:4)
您的表单中有promotores
字段链接到模型,因此save()
会尝试将其保存到m2m字段。您可以使用commit=False
保存表单,然后手动保存对象(它不会触及m2m),然后像现在一样保存m2m。
P.S。我想你应该使用form.cleaned_data['promotores']
而不是request.POST。上帝知道客户实际发布了什么。
答案 1 :(得分:4)
用于保存与中间类(通过)的ManyToMany关系的解决方案:
def add_setor(request):
form = SetorForm(request.POST or None)
if form.is_valid():
setor = Setor.objects.create(nome=form.cleaned_data['nome'])
for promotor_id in request.POST.getlist('promotores'):
m1 = Membro(promotor_id=promotor_id, setor=setor)
m1.save()
messages.add_message(request, messages.SUCCESS, 'Setor cadastrado com sucesso!')
return HttpResponseRedirect('/project/setor/index/')
return render_to_response('project/setor/form.html', locals(), context_instance=RequestContext(request))
我没有直接使用表单,但提取表单的setor名称然后创建对象setor。使用表单和对象setor中的id的promotores创建Membro对象,最后一个操作是保存Membro对象。 问候。