具有Parler翻译的Django Admin生成IntegrityError

时间:2018-10-02 10:34:09

标签: django django-models django-parler

我正在使用Django Parler 1.9.2和Django 2.1。 我有一个带有内联的Business Admin类,可以编辑相关模型(服务和资源)

class ServiceInline(TranslatableTabularInline):
    model = Service

class ResourceInline(TranslatableTabularInline):
    model = Resource

@admin.register(Business)
class BusinessAdmin(TranslatableAdmin):
    inlines = [ServiceInline, ResourceInline]

当我尝试在Admin中编辑业务时,它会给我一个IntegrityError:

File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/forms/models.py", line 650, in save_existing
  return form.save(commit=commit)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/forms/models.py", line 458, in save
  self.instance.save()
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/parler/models.py", line 648, in save
  self.save_translations(*args, **kwargs)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/parler/models.py", line 701, in save_translations
  self.save_translation(translation, *args, **kwargs)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/parler/models.py", line 728, in save_translation
  translation.save(*args, **kwargs)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/base.py", line 717, in save
  force_update=force_update, update_fields=update_fields)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/parler/models.py", line 892, in save_base
  super(TranslatedFieldsModelMixin, self).save_base(raw=raw, using=using, **kwargs)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/base.py", line 747, in save_base
  updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/base.py", line 833, in _save_table
  result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/base.py", line 872, in _do_insert
  using=using, raw=raw)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
  return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/query.py", line 1133, in _insert
  return query.get_compiler(using=using).execute_sql(return_id)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1285, in execute_sql
  cursor.execute(sql, params)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
  return super().execute(sql, params)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
  return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
  return executor(sql, params, many, context)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
  return self.cursor.execute(sql, params)
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
  raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/vagrant/.virtualenvs/appy/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
  return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "businesses_resource_tran_language_code_master_id_598f5fae_uniq"
DETAIL:  Key (language_code, master_id)=(en, 9) already exists.

有什么解决方法的想法吗?

1 个答案:

答案 0 :(得分:0)

已解决。 A是数据库触发器。该触发器在执行save方法之前但在创建对象之后创建了一个新条目。我认为这是Django的局限性。