【问题标题】:Django: Using Third party app as a django appDjango:将第三方应用程序用作 django 应用程序
【发布时间】:2014-08-18 14:44:28
【问题描述】:

之前我在虚拟环境中使用django-taggit 应用程序。现在我需要添加一个 自定义字段外键(Profile)为 taggit 应用程序的Tag 建模。 我将 taggit 应用程序从虚拟环境移动到应用程序目录并交叉检查该应用程序的路径:

<module 'taggit' from '/home/user/VIR/poll/taggit/__init__.pyc'>

此时标记似乎工作正常。

Tag 模型中进行了以下更改:

from profile.models import Profile

class Tag(TagBase):
    profile = models.ForeignKey(Profile)
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

在运行schemamigration 时出现错误:

ImportError: cannot import name Profile

有什么见解要发布吗?

【问题讨论】:

  • 建议您不要直接更改第 3 方模块。您可以做的一件事是,创建一个新模块,并将 Tag 模型扩展为 CustomTag 或其他东西..

标签: django django-south


【解决方案1】:

也许您在profile.models 中使用Tag,因此存在循环导入,Python 可以处理它。

如果您想防止循环导入问题,您可以删除 from profile.models import Profile 并使用外键的惰性模型定义:

class Tag(TagBase):
    profile = models.ForeignKey('profile.Profile')

我不知道您的情况的详细信息,但我认为您使用 django-taggit 不正确。默认情况下,标签使用通用外键,您不需要显式外键来指向您的模型http://django-taggit.readthedocs.org/en/latest/getting_started.html

【讨论】:

  • 是的 GFK 被 TaggedItem 使用...但我的情况似乎适合进行该更改。
猜你喜欢
  • 2011-04-25
  • 1970-01-01
  • 2017-03-12
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2020-07-14
  • 1970-01-01
相关资源
最近更新 更多