Django迁移 - 如何让它忘记? [英] Django migrations - how to make it forget?

查看:141
本文介绍了Django迁移 - 如何让它忘记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在草拟一个新的Django应用程序,其运行在后台窗口中的runserver dev服务器可以跟踪网络布线,而简单的 在我的模型中有这样的一个例子:

I've been sketching out a new Django app with the runserver dev server running in a background window to tracking network wiring, and briefly had this in my model:

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)

在我意识到我不能两个字段的相关名称相同。我想我将需要写一些特殊的东西来找到与接口相关的所有连接,因为它们可能是连接的到或从结束(对有更好的方式的任何见解感兴趣 - 像一个设置字段)

Before I realised I couldn't have the same related_name for two fields. I guess I will need to write something special to find all connections related to an interface, since they might be the 'to' or 'from' end of the connection (be interested in any insight on a nicer way to do that - like a "Set" field)

在这一点上,我没有做过makemigrations,但是在停止服务器并进行迁移时,我得到:

At this point, I hadn't done a makemigrations, but on stopping the server, and making my migrations, I get:

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'.

即使没有冲突了。我没有在任何地方看到一个迁移目录 - 这是模型的初始通过 - 所以在重新启动dev服务器之后,这个错误的内存在哪里?

Even though there isn't a clash anymore. I don't see a migrations directory anywhere - this was the initial pass at the models - so where is the memory of this error coming from after a restart of the dev server?

编辑:为了更清楚,我的连接模型现在看起来像:

To make it clearer, my Connection model now looks like:

class Connection(models.Model):
    interface_from = models.ForeignKey(Interface)
    interface_to = models.ForeignKey(Interface)
    source = models.CharField(max_length=32)


推荐答案

在第一个例子中:

class Connection(models.Model):
    interface_from = models.ForeignKey(Interface, related_name="connections")
    interface_to = models.ForeignKey(Interface, related_name="connections")

您正在告诉Django创建两个不同的连接 接口的属性返回到连接,其中显然不起作用。

You're telling Django to create two different connections attributes on Interface for the backwards relationships back to Connection, which obviously doesn't work.

在第二个例子中:

class Connection(models.Model):
    interface_from = models.ForeignKey(Interface)
    interface_to = models.ForeignKey(Interface)

您正在告诉Django使用其默认的 connections_set 名称,用于向后关系的两个不同属性返回到连接,这也不起作用。

You're telling Django to use its default connections_set name for two different attributes for the backwards relationships back to Connection, which also doesn't work.

修复是使用 related_name ='+'(as @Ivan表示)完全禁用向后关系,或者两个显式提供两个不同的 related_name 属性,使后向关系属性的名称不会冲突。

The fix is to use related_name='+' (as @Ivan said) to disable the backwards relationships completely or two explicitly provide two different related_name attributes so that the backwards relationship attributes' names don't clash.

这篇关于Django迁移 - 如何让它忘记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆