超类中的外键在进行迁移时会导致冲突

时间:2016-08-17 14:58:04

标签: django django-models

Django 1.10

我只是想在一般模型中统一一个公共字段,然后继承它。我不知道我怎么能坚持DRY原则。但我无法进行迁移。你能在这里给我一些建议吗?

class GeneralModel(models.Model):
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="created_by")

    class Meta:
        abstract = True

class Image(GeneralModel):
    ...

class Masterphoto(GeneralModel):
    ...

回溯

python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
image.Image.created_by: (fields.E304) Reverse accessor for 'Image.created_by' clashes with reverse accessor for 'Masterphoto.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Image.created_by' or 'Masterphoto.created_by'.
image.Image.created_by: (fields.E305) Reverse query name for 'Image.created_by' clashes with reverse query name for 'Masterphoto.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Image.created_by' or 'Masterphoto.created_by'.
masterphoto.Masterphoto.created_by: (fields.E304) Reverse accessor for 'Masterphoto.created_by' clashes with reverse accessor for 'Image.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Masterphoto.created_by' or 'Image.created_by'.
masterphoto.Masterphoto.created_by: (fields.E305) Reverse query name for 'Masterphoto.created_by' clashes with reverse query name for 'Image.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Masterphoto.created_by' or 'Image.created_by'.

1 个答案:

答案 0 :(得分:0)

这与迁移无关。

相关名称必须是唯一的。 model inheritance docs解释了在这种情况下该做什么;您可以使用'%(app_label)s''%(class)s'自动将相关名称插入生成的值中。