在syncdb之前,从标准Django模型中删除字段

发布于 2024-12-20 17:30:40 字数 1376 浏览 3 评论 0原文

这是关于从标准 Django 模型中删除字段的后续问题。简而言之:可以从已创建的模型中动态删除字段,在本例中为 User.email 字段。因此,字段电子邮件将从用户中删除,而不更改用户的代码。例如,请参阅下面的代码。

我可以从模型(1)中动态删除字段,但这种情况在服务器启动时发生,并且在服务器存在时被撤消。由于syncdb 不需要服务器运行,并且通常似乎会忽略删除代码(以某种方式),因此这种方法不会阻止该字段出现在数据库中(2)。

有没有办法从模型中删除该字段(不更改它所在的文件,因为它是 Django 模型),同时也使其不会出现在数据库中?

提前致谢!

标记

编辑:我的问题是不是,我正在从模型文件中删除文本“m = models.IntegerField()”并希望从数据库中删除字段。问题是我正在使用下面的代码从模型中删除已在另一个文件中声明的字段。我不认为每次运行syncdb 时都使用South 创建迁移是一个解决方案(3)。

其他信息:

  • 1)目前,代码位于 models.py 中,但我想 在哪里放置 Django 启动代码? 有效。
  • 2)我可以使用自定义查询在 post_syncdb 信号上删除它,但我希望有更优雅的东西......或者更准确地说,完全优雅。
  • 3)如果它真的有效的话,因为显然syncdb仍然看到“已删除”字段),所以我认为South会这样做,因为它仍然以某种方式存在。

这是代码(models.py):

class A(models.Model):
    m = models.IntegerField()

for i, f in enumerate(A._meta.fields):
    if f.name == 'm':
        del A._meta.fields[i]
        break

class B(A):
    n = models.IntegerField()

for i, f in enumerate(B._meta.fields):
    if f.name == 'm':
        del B._meta.fields[i]
        break

编辑:我检查(使用打印)并且删除代码上执行在syncdb上。它在创建表之前执行

This is a follow-up question on Delete field from standard Django model. In short: a field can be dynamically deleted from a model that is already created, in this case the field User.email . So field email would be deleted from User without changing the code for User. See code below for example.

I can dynamically delete a a field from a model(1), but that happens when the server starts and is undone when it exists. Since syncdb doesn't require the server to be running, and generally seems to ignore the deletion code (somehow), this approach doesn't prevent the field from appearing in the database(2).

Is there a way to do delete the field from the model (without changing the file it's in, as it's a Django model), in a way that also makes it not appear in the database?

Thanks in advance!

Mark

EDIT: I problem is not that I am deleting the text "m = models.IntegerField()" from the model file and want the field removed from the database. The problem is that I am using the code below to remove a field from a model that has already been declared in another file. I do not think that creating a migration with South for every time I run syncdb is a solution(3).

Additional information:

  • 1) Currently, the code is in models.py, but I suppose Where to put Django startup code? works.
  • 2) I can delete it on post_syncdb signal with custom query, but I hope for something more elegant... Or elegant at all, more accurately.
  • 3) If it even works at all, because obviously syncdb is still seeing the 'removed' field), so I think South would to as it's still somehow there.

This is the code (models.py):

class A(models.Model):
    m = models.IntegerField()

for i, f in enumerate(A._meta.fields):
    if f.name == 'm':
        del A._meta.fields[i]
        break

class B(A):
    n = models.IntegerField()

for i, f in enumerate(B._meta.fields):
    if f.name == 'm':
        del B._meta.fields[i]
        break

EDIT: I checked (with print) and the deletion code is executed on syncdb. It is executed before tables are created

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-12-27 17:30:40

django 做了很多元类魔法,我猜元类负责定义数据库表来支持你的模型。随后,仅删除该字段不足以更改生成的表。

正如一些人指出的那样, south 是解决这些问题的方法。

django does a lot of meta class magic and i would guess that the meta class is responsible for defining the database table to back your model. Subsequently just deleting the field is not enough to alter the generated table.

as several people have pointed out, south is the way to deal with these problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文