Django重复输入错误的原因(1062)?

时间:2013-10-10 18:49:14

标签: django django-models django-forms django-errors

我更新了以下信息,以引用给我同样错误的不同模型/视图。它是一个更简单的模型,因此需要考虑的变量较少......

我有以下型号:

class Imaging_order(Order):
    order_description = models.ForeignKey(Imaging_test, limit_choices_to = {'active': 1}, null=True, blank=True)
    orders = models.ManyToManyField(Imaging_test, limit_choices_to = {'active': 1}, related_name='orders')
    ...

我有以下视图(基本上是基于django类的通用更新视图,其中混合了一些自定义函数):

class LoginRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

class EditMixin(object):
    def form_valid(self, form):
        messages.success(self.request, "This %s has been updated." % (self.model._meta.verbose_name), extra_tags='msg')
        return super(EditMixin, self).form_valid(form)

class ImagingMixin(LoginRequiredMixin):
    model = Imaging_order
    form_class = ImagingForm

class ImagingUpdateView(ImagingMixin, EditMixin, UpdateView):
    pass

在我们的实时版本上,我偶尔会收到此错误的通知:

    Traceback (most recent call last):

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 47, in view
    return self.dispatch(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 28, in _wrapper
    return bound_func(*args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/utils/decorators.py", line 24, in bound_func
    return func(self, *args2, **kwargs2)

  File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 150, in dispatch
    return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/base.py", line 68, in dispatch
    return handler(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 195, in post
    return super(BaseUpdateView, self).post(request, *args, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 138, in post
    return self.form_valid(form)

  File "/home/cpcadmin/webapps/cpcdj2/cpc/emr/views.py", line 155, in form_valid
    return super(EditMixin, self).form_valid(form)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/views/generic/edit.py", line 112, in form_valid
    self.object = form.save()

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 363, in save
    fail_message, commit, construct=False)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 86, in save_instance
    save_m2m()

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/forms/models.py", line 82, in save_m2m
    f.save_form_data(instance, cleaned_data[f.name])

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 1149, in save_form_data
    setattr(instance, self.attname, data)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 746, in __set__
    manager.add(*value)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 503, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/fields/related.py", line 587, in _add_items
    '%s_id' % target_field_name: obj_id,

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 360, in create
    obj.save(force_insert=True, using=self.db)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)

  File "/home/cpcadmin/webapps/cpcdj2/lib/python2.7/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)

  File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)

  File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue

IntegrityError: (1062, "Duplicate entry '25129-18' for key 2")

我无法判断此错误何时发生(通常此视图工作正常)。我想我有一个普通的模型/视图/表单设置,除了我扩展基于类的基于通用的视图。我同意以下评论,这与m2m字段有关。每次出现此错误时,它都会调用“save_m2m”,然后会导致此错误。我仍然无法重现它。它发生在我的更新页面/视图中,所以我认为只有在使用现有相关记录更新现有模型时才会这样。这是我能找到的最接近的其他帖子,但没有帮助:IntegrityError: (1062, "Duplicate entry '1830327-1792993' for key 'some_instance_A_id'") but no UNIQUE constraint

0 个答案:

没有答案