【发布时间】:2023-04-06 10:38:01
【问题描述】:
我一直在草拟一个新的 Django 应用程序,其中 runserver 开发服务器在后台窗口中运行以跟踪网络布线,简要地在我的模型中有这个:
class Interface(models.Model):
name = models.CharField(max_length=200)
# (blah)
class Connection(models.Model):
interface_from = models.ForeignKey(Interface, related_name="connections")
interface_to = models.ForeignKey(Interface, related_name="connections")
source = models.CharField(max_length=32)
在我意识到两个字段不能有相同的related_name 之前。我想我需要写一些特别的东西来查找与接口相关的所有连接,因为它们可能是连接的“到”或“从”端(有兴趣了解更好的方法来做到这一点 - 比如“设置”字段)
此时,我还没有进行迁移,但是在停止服务器并进行迁移时,我得到:
ERRORS:
autodoc.Connection.interface_from: (fields.E304) Reverse accessor for 'Connection.interface_from' clashes with reverse accessor for 'Connection.interface_to'.
HINT: Add or change a related_name argument to the definition for 'Connection.interface_from' or 'Connection.interface_to'.
即使不再有冲突。我在任何地方都没有看到迁移目录 - 这是模型的初始传递 - 那么在重新启动开发服务器后,这个错误的内存来自哪里?
编辑:为了更清楚,我的连接模型现在看起来像:
class Connection(models.Model):
interface_from = models.ForeignKey(Interface)
interface_to = models.ForeignKey(Interface)
source = models.CharField(max_length=32)
【问题讨论】:
-
更新后的
Connection模型是什么样的? -
更改related_name,不能相等
-
@JoshKelley - 为您更新了问题
-
@PauloPessoa - 我知道,这就是我再次删除它的原因! :-)
-
尝试只在一个字段中添加related_name
标签: python django django-migrations