我需要阻止用户从模型中的管理员列表中自我杀死:
class Organization(models.Model):
administrators = models.ManyToManyField(User, blank=True, null=True, help_text=_('Administrators are people that manage the organization'))
def save(self, *args, **kwargs):
# --- some specific code here ---
super(Organization, self).save(*args, **kwargs)
if self.user_id not in self.administrators.values_list('id', flat=True):
self.administrators.add(self.user)
# super(Organization, self).save(*args, **kwargs)
self.save()
# assert False, self.administrators.all() # <- it works, if assert goes here
好吧,这可能是一些黑魔法,让我们试试post_save
信号:
def organization_post_save(sender, instance, created, **kwargs):
if instance.user_id not in instance.administrators.all().values_list('id', flat=True):
instance.administrators.add(instance.user)
instance.save()
# assert False, '?'
仅在断言发生时才在管理员列表中添加用户。好吧,可能是黑魔法再次发生,让我们试试:
def organization_m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwargs):
if instance.user_id not in instance.administrators.all().values_list('id', flat=True):
instance.administrators.add(instance.user_id)
m2m_changed.connect(organization_m2m_changed, sender=Organization.administrators.through)
当然,maximum recursion depth exceeded
。怎么了?这种痛苦是不可阻挡的:(
似乎保存了名为 BEFORE m2m关系的post_save
方法,因此它遇到了竞争条件,并且新表格数据被替换为空数据。这是一个糟糕的解决方案:
def organization_m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwargs):
if not instance.administrators.filter(id=instance.user_id).exists():
if action.startswith('post_'):
instance.administrators.add(instance.user)
现在我想知道Django在完成 ALL 工作后会发出什么信号?
P.S。没有魔法。 :(
forms.py
class OrganizationEditForm(forms.ModelForm):
class Meta:
model = Organization
exclude = ['user']
widgets = floppyforms_widgets(Organization)
views.py
@login_required
def edit_organization(request, organization_id=None):
user = request.user
c = Context({'user': user})
instance = get_object_or_404(Organization, id=organization_id) if organization_id else Organization(user=user)
form = OrganizationEditForm(request.POST or None, request.FILES or None, instance=instance)
if form.is_valid():
form.save()
messages.success(request, _('Organization saved successfully'))
return HttpResponseRedirect(reverse('organizations'))
c['form'] = form
c['instance'] = instance
return render_to_response('cat/edit_organization.html', c, context_instance=RequestContext(request))
答案 0 :(得分:0)
您收到递归错误,因为您在Organization.save()中调用self.save()。不要这样做,如果你想在覆盖save()时保存,请调用superclass的save()版本而不是你自己的save()。
在将实例添加到m2m关系之前,关系的两端必须已经具有主键(即已经插入到db中)。这意味着您无法将未保存的未创建对象添加到m2m关系中。
这样做:
organisation.save()
user.save()
organisation.administrators.add(user)
我建议在View或Form中执行此操作,因为如果模型的save方法也保存了用户的模型(尽管它应该在模型的save()中工作),这是非常意外的。