当前位置:   article > 正文

django的数据库模型中的域多次引用同一个外键(表)_django 一个表引用相同的外键

django 一个表引用相同的外键

执行python manage makemigrations OnlineUpgrade时,报如下错误:

  1. <span style="color:#ff6666;">D:\KLServer\pycode\KLServer>python manage.py makemigrations OnlineUpgrade
  2. SystemCheckError: System check identified some issues:
  3. ERRORS:
  4. OnlineUpgrade.SoftwareUpgradeRecord.high_version: (fields.E304) Reverse accessor for 'SoftwareUpgradeRecord.high_version' clashes with reverse accessor for 'SoftwareVersionDiff.high_version'.
  5. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.high_version' or 'SoftwareVersionDiff.high_version'.
  6. OnlineUpgrade.SoftwareUpgradeRecord.high_version: (fields.E304) Reverse accessor for 'SoftwareUpgradeRecord.high_version' clashes with reverse accessor for 'SubSoftwareKitVersionDiff.high_version'.
  7. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.high_version' or 'SubSoftwareKitVersionDiff.high_version'.
  8. OnlineUpgrade.SoftwareUpgradeRecord.high_version: (fields.E305) Reverse query name for 'SoftwareUpgradeRecord.high_version' clashes with reverse query name for 'SoftwareVersionDiff.high_version'.
  9. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.high_version' or 'SoftwareVersionDiff.high_version'.
  10. OnlineUpgrade.SoftwareUpgradeRecord.high_version: (fields.E305) Reverse query name for 'SoftwareUpgradeRecord.high_version' clashes with reverse query name for 'SubSoftwareKitVersionDiff.high_version'.
  11. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.high_version' or 'SubSoftwareKitVersionDiff.high_version'.
  12. OnlineUpgrade.SoftwareUpgradeRecord.low_version: (fields.E304) Reverse accessor for 'SoftwareUpgradeRecord.low_version' clashes with reverse accessor for 'SoftwareVersionDiff.low_version'.
  13. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.low_version' or 'SoftwareVersionDiff.low_version'.
  14. OnlineUpgrade.SoftwareUpgradeRecord.low_version: (fields.E305) Reverse query name for 'SoftwareUpgradeRecord.low_version' clashes with reverse query name for 'SoftwareVersionDiff.low_version'.
  15. HINT: Add or change a related_name argument to the definition for 'SoftwareUpgradeRecord.low_version' or 'SoftwareVersionDiff.low_version'.
  16. OnlineUpgrade.SoftwareVersionDiff.high_version: (fields.E304) Reverse accessor for 'SoftwareVersionDiff.high_version' clashes with reverse accessor for 'SoftwareUpgradeRecord.high_version'.
  17. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.high_version' or 'SoftwareUpgradeRecord.high_version'.
  18. OnlineUpgrade.SoftwareVersionDiff.high_version: (fields.E304) Reverse accessor for 'SoftwareVersionDiff.high_version' clashes with reverse accessor for 'SubSoftwareKitVersionDiff.high_version'.
  19. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.high_version' or 'SubSoftwareKitVersionDiff.high_version'.
  20. OnlineUpgrade.SoftwareVersionDiff.high_version: (fields.E305) Reverse query name for 'SoftwareVersionDiff.high_version' clashes with reverse query name for 'SoftwareUpgradeRecord.high_version'.
  21. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.high_version' or 'SoftwareUpgradeRecord.high_version'.
  22. OnlineUpgrade.SoftwareVersionDiff.high_version: (fields.E305) Reverse query name for 'SoftwareVersionDiff.high_version' clashes with reverse query name for 'SubSoftwareKitVersionDiff.high_version'.
  23. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.high_version' or 'SubSoftwareKitVersionDiff.high_version'.
  24. OnlineUpgrade.SoftwareVersionDiff.low_version: (fields.E304) Reverse accessor for 'SoftwareVersionDiff.low_version' clashes with reverse accessor for 'SoftwareUpgradeRecord.low_version'.
  25. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.low_version' or 'SoftwareUpgradeRecord.low_version'.
  26. OnlineUpgrade.SoftwareVersionDiff.low_version: (fields.E305) Reverse query name for 'SoftwareVersionDiff.low_version' clashes with reverse query name for 'SoftwareUpgradeRecord.low_version'.
  27. HINT: Add or change a related_name argument to the definition for 'SoftwareVersionDiff.low_version' or 'SoftwareUpgradeRecord.low_version'.
  28. OnlineUpgrade.SubSoftwareKitVersionDiff.high_version: (fields.E304) Reverse accessor for 'SubSoftwareKitVersionDiff.high_version' clashes with reverse accessor for 'SoftwareUpgradeRecord.high_version'.
  29. HINT: Add or change a related_name argument to the definition for 'SubSoftwareKitVersionDiff.high_version' or 'SoftwareUpgradeRecord.high_version'.
  30. OnlineUpgrade.SubSoftwareKitVersionDiff.high_version: (fields.E304) Reverse accessor for 'SubSoftwareKitVersionDiff.high_version' clashes with reverse accessor for 'SoftwareVersionDiff.high_version'.
  31. HINT: Add or change a related_name argument to the definition for 'SubSoftwareKitVersionDiff.high_version' or 'SoftwareVersionDiff.high_version'.
  32. OnlineUpgrade.SubSoftwareKitVersionDiff.high_version: (fields.E305) Reverse query name for 'SubSoftwareKitVersionDiff.high_version' clashes with reverse query name for 'SoftwareUpgradeRecord.high_version'.
  33. HINT: Add or change a related_name argument to the definition for 'SubSoftwareKitVersionDiff.high_version' or 'SoftwareUpgradeRecord.high_version'.
  34. OnlineUpgrade.SubSoftwareKitVersionDiff.high_version: (fields.E305) Reverse query name for 'SubSoftwareKitVersionDiff.high_version' clashes with reverse query name for 'SoftwareVersionDiff.high_version'.
  35. HINT: Add or change a related_name argument to the definition for 'SubSoftwareKitVersionDiff.high_version' or 'SoftwareVersionDiff.high_version'.</span>



原来的模型代码:

  1. @python_2_unicode_compatible
  2. class SoftwareVersionDiff(models.Model):
  3. '''
  4. 软件版本差分
  5. '''
  6. # 软件
  7. software = models.ForeignKey(to=Software, verbose_name='软件')
  8. # 低版本
  9. low_version = models.ForeignKey(to=Version,
  10. related_name='low_version', verbose_name='低版本')
  11. # 高版本
  12. high_version = models.ForeignKey(to=Version,
  13. related_name='high_version', verbose_name='高版本')
  14. @python_2_unicode_compatible
  15. class SubSoftwareKitVersionDiff(models.Model):
  16. '''
  17. 软件子集版本差分
  18. '''
  19. # 软件子集
  20. subsoftwarekit = models.ForeignKey(to=SubSoftwareKit, verbose_name='软件子集')
  21. # 低版本
  22. low_version = models.ForeignKey(to=Version,
  23. related_name='low_versin_subsoftware', verbose_name='低版本')
  24. # 高版本
  25. high_version = models.ForeignKey(to=Version,
  26. related_name='high_version',verbose_name='高版本')
  27. @python_2_unicode_compatible
  28. class SoftwareUpgradeRecord(models.Model):
  29. '''
  30. 软件更新记录,记录更新过程
  31. 这里特别需要注意的是,一次更新请求中可能会有许多的软件需要更新,这个时候一次请求,可能会
  32. 触发多次更新记录,因为每个软件更新的(低版本->高版本)是不一样的。
  33. '''
  34. # 更新的所有软件
  35. softwares = models.CharField(verbose_name='软件', max_length=2048, null=False)
  36. # 低版本
  37. low_version = models.ForeignKey(to=Version,
  38. related_name='low_version', verbose_name='低版本')
  39. # 高版本
  40. high_version = models.ForeignKey(to=Version,
  41. related_name='high_version', verbose_name='高版本')

原因: 因为在一个表中多次引用某个外键表,而且没有指定唯一的releated_name,而导致的。

解决方案:将同一个表中的releated_name,全部修改,指定不一样的releated_name

修改后的代码

  1. @python_2_unicode_compatible
  2. class SoftwareVersionDiff(models.Model):
  3. '''
  4. 软件版本差分
  5. '''
  6. # 软件
  7. software = models.ForeignKey(to=Software, verbose_name='软件')
  8. # 低版本
  9. low_version = models.ForeignKey(to=Version,
  10. related_name='low_version_software_version_diff', verbose_name='低版本')
  11. # 高版本
  12. high_version = models.ForeignKey(to=Version,
  13. related_name='high_version_software_version_diff', verbose_name='高版本')
  14. @python_2_unicode_compatible
  15. class SubSoftwareKitVersionDiff(models.Model):
  16. '''
  17. 软件子集版本差分
  18. '''
  19. # 软件子集
  20. subsoftwarekit = models.ForeignKey(to=SubSoftwareKit, verbose_name='软件子集')
  21. # 低版本
  22. low_version = models.ForeignKey(to=Version,
  23. related_name='low_versin_subsoftware_version_diff', verbose_name='低版本')
  24. # 高版本
  25. high_version = models.ForeignKey(to=Version,
  26. related_name='high_version_subsoftware_version_diff',verbose_name='高版本')
  27. @python_2_unicode_compatible
  28. class SoftwareUpgradeRecord(models.Model):
  29. '''
  30. 软件更新记录,记录更新过程
  31. 这里特别需要注意的是,一次更新请求中可能会有许多的软件需要更新,这个时候一次请求,可能会
  32. 触发多次更新记录,因为每个软件更新的(低版本->高版本)是不一样的。
  33. '''
  34. # 更新的所有软件
  35. softwares = models.CharField(verbose_name='软件', max_length=2048, null=False)
  36. # 低版本
  37. low_version = models.ForeignKey(to=Version,
  38. related_name='low_version_software_upgrade_record', verbose_name='低版本')
  39. # 高版本
  40. high_version = models.ForeignKey(to=Version,
  41. related_name='high_version_software_upgrade_record', verbose_name='高版本')





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/306423
推荐阅读
相关标签
  

闽ICP备14008679号