Python Django:由于唯一约束,通过表单更新模型失败

Python Django:由于唯一约束,通过表单更新模型失败,python,django,forms,unique-constraint,Python,Django,Forms,Unique Constraint,我试图在传递模型实例时通过表单更新模型 form = forms.ProfileForm(params, instance=profile) if not form.is_valid(): print form.errors 但它在以下方面失败了: “此应用和电子邮件的配置文件已存在。” 以下是参数: params = {'first_name': u'foo', 'last_name': u'bar', 'app': 1, u'id': 12349, 'phone': u'999',

我试图在传递模型实例时通过表单更新模型

form = forms.ProfileForm(params, instance=profile)
if not form.is_valid():
    print form.errors
但它在以下方面失败了:

“此应用和电子邮件的配置文件已存在。”

以下是参数

params = {'first_name': u'foo', 'last_name': u'bar', 'app': 1, u'id': 12349, 'phone': u'999', 'email': u'foo@bar.com'}
profile = Profile.objects.get(pk=12349)
model_to_dict(profile)

{'first_name': u'foo', 'last_name': u'bar', 'app': 1, u'id': 12349, 'phone': u'12345', 'email': u'foo@bar.com'}
模型

params = {'first_name': u'foo', 'last_name': u'bar', 'app': 1, u'id': 12349, 'phone': u'999', 'email': u'foo@bar.com'}
profile = Profile.objects.get(pk=12349)
model_to_dict(profile)

{'first_name': u'foo', 'last_name': u'bar', 'app': 1, u'id': 12349, 'phone': u'12345', 'email': u'foo@bar.com'}
model.py

class Profile(models.Model):
    app = models.ForeignKey('core.App', null=False)
    email = models.EmailField(null=True, blank=True, default=None)
    first_name = models.CharField(max_length=64, default=None, null=True)
    last_name = models.CharField(max_length=64, default=None, null=True)
    phone = models.CharField(max_length=32, null=True, blank=True)

    class Meta:
        unique_together = (('app', 'email'),)
class ProfileForm(forms.ModelForm):
    class Meta:
        model = models.Profile
forms.py

class Profile(models.Model):
    app = models.ForeignKey('core.App', null=False)
    email = models.EmailField(null=True, blank=True, default=None)
    first_name = models.CharField(max_length=64, default=None, null=True)
    last_name = models.CharField(max_length=64, default=None, null=True)
    phone = models.CharField(max_length=32, null=True, blank=True)

    class Meta:
        unique_together = (('app', 'email'),)
class ProfileForm(forms.ModelForm):
    class Meta:
        model = models.Profile
Django在调用is\u valid()时在表单上执行名为*validate\u unique*的函数。 但是为什么呢?它在插入时有意义,但在更新时没有意义

在调用is_valid()之前,我可以在执行此操作时进行变通:


有合适的解决方案吗?

我们需要视图,我想django是在尝试插入,而不是更新,以防django猜测到插入,它检查唯一约束?如果更新不正确,是否正确?它在上失败,但在保存()时无效。我不能发布视图,它太复杂了,但所需的一切都显示在我的帖子中。我可以在没有视图的情况下在shell中复制错误。