带有 Parler 翻译的 Django Admin 生成 IntegrityError
Django Admin with Parler translations generates IntegrityError
我正在使用 Django Parler 1.9.2 和 Django 2.1。
我有一个业务管理员 Class 可以内联编辑相关模型(服务和资源)
class ServiceInline(TranslatableTabularInline):
model = Service
class ResourceInline(TranslatableTabularInline):
model = Resource
@admin.register(Business)
class BusinessAdmin(TranslatableAdmin):
inlines = [ServiceInline, ResourceInline]
当我尝试在管理员中编辑业务时,出现 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.
知道如何解决这个问题吗?
已解决。 A 是一个数据库触发器。此触发器在执行保存方法之前但在创建对象之后创建了一个新条目。我认为这是 Django 的限制。
我正在使用 Django Parler 1.9.2 和 Django 2.1。 我有一个业务管理员 Class 可以内联编辑相关模型(服务和资源)
class ServiceInline(TranslatableTabularInline):
model = Service
class ResourceInline(TranslatableTabularInline):
model = Resource
@admin.register(Business)
class BusinessAdmin(TranslatableAdmin):
inlines = [ServiceInline, ResourceInline]
当我尝试在管理员中编辑业务时,出现 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.
知道如何解决这个问题吗?
已解决。 A 是一个数据库触发器。此触发器在执行保存方法之前但在创建对象之后创建了一个新条目。我认为这是 Django 的限制。