【发布时间】:2018-09-06 23:01:45
【问题描述】:
Python 3.6 Django 2.0
我有两个模型:
from django.contrib.contenttypes.fields import GenericRelation
@python_2_unicode_compatible
class Domain(models.Model):
tool_result = GenericRelation('projects.ToolResult')
@python_2_unicode_compatible
class ToolResult(models.Model):
tool = models.ForeignKey('projects.Tool', on_delete=models.CASCADE)
class Meta:
unique_together = ('content_type', 'object_id', 'tool')
然后在管理类中创建通用表格内联:
class ToolResultGenericTabularInline(GenericTabularInline):
model = ToolResult
extra = 0
@admin.register(Domain)
class DomainAdmin(admin.ModelAdmin):
inlines = [ToolResultGenericTabularInline, ]
当我尝试通过内联保存相同的对象时,出现错误:
重复键值违反唯一约束 “projects_toolresult_content_type_id_object_i_71ee2c2e_uniq”详细信息: 键 (content_type_id, object_id, tool_id)=(18, 22, 3) 已存在。
这是一个 django 错误吗?
【问题讨论】:
-
您尝试过使用新数据库吗?
-
是的,当我不是通过内联创建对象时,它工作正常