django-south - 降级依赖项的正确方法?
我正在使用 django 构建一个应用程序,并使用 django-south 进行数据库架构迁移。我正在使用 django-mptt 构建评论系统,并且我安装了 0.5-pre (当前的 git master 分支)。
我正在使用的版本有一个名为 TreeForeignKey
的 django 字段,但我正在尝试测试 0.5 是否存在 0.4 中存在的错误,因此我删除了我的 django-mptt 版本并安装了奶酪店的当前版本。我修改了代码以使用 ForeignKey
而不是 TreeForeignKey
。
当需要进行迁移时,它显然会中断此消息:
ValueError: Cannot import the required field 'mptt.fields.TreeForeignKey'
所有我的迁移文件都引用 mptt.fields.TreeForeignKey ,这在 django-mptt 0.4 中不存在。
我在 django-mptt 0.5 中的 Comment
模型:
from mptt.models import MPTTModel
from mptt.fields import TreeForeignKey
class Comment(MPTTModel):
# ...
parent = TreeForeignKey('self', related_name='children', blank=True, null=True)
降级到 django-mptt 0.4 后的模型相同
import django.db.models
from mptt.models import MPTTModel
class Comment(MPTTModel):
# ... cruft
# TreeForeignKey does not exist in mptt 0.5!
parent = models.ForeignKey('self', related_name='children', blank=True, null=True)
我想出了两种相当老套的方法来修复此问题并允许迁移工作:
- 添加 < code>TreeForeignKey 类到我的 django-mptt 安装。
- 修改我的迁移文件以删除对
mptt.fields.TreeForeignKey
的引用。
我采用了第一种方法并且它有效,但我觉得这是一种黑客(但不如第二种方法)。
有没有一种非 hacky 的方法可以完成我对降级依赖项所做的操作,从而导致模型中的某些字段发生变化?
I am building an application using django, and using django-south for the database schema migrations. I'm using django-mptt to build a comment system, and I installed 0.5-pre (the current git master branch).
The version I'm using has a django-field called TreeForeignKey
, but I'm trying to test whether or not 0.5 has a bug that exists in 0.4, so I removed my version of django-mptt and installed the current release from the cheeseshop. I modified my code to use ForeignKey
rather than TreeForeignKey
.
When it comes time to do migrations, it obviously breaks with this message:
ValueError: Cannot import the required field 'mptt.fields.TreeForeignKey'
All my migration files reference mptt.fields.TreeForeignKey
, which doesn't exist in django-mptt 0.4.
My Comment
model in django-mptt 0.5:
from mptt.models import MPTTModel
from mptt.fields import TreeForeignKey
class Comment(MPTTModel):
# ...
parent = TreeForeignKey('self', related_name='children', blank=True, null=True)
The same model after I downgrade to django-mptt 0.4
import django.db.models
from mptt.models import MPTTModel
class Comment(MPTTModel):
# ... cruft
# TreeForeignKey does not exist in mptt 0.5!
parent = models.ForeignKey('self', related_name='children', blank=True, null=True)
There are two rather hacky approaches I came up with to fix this and allow the migration to work:
- Add the
TreeForeignKey
class to my django-mptt install. - Modify my migration files to remove references to
mptt.fields.TreeForeignKey
.
I took the first approach and it worked, but I feel it's kind of a hack (but not as much as the second option).
Is there a non-hacky way to do what I did with downgrading dependencies that resulted in some fields changing in my models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这确实与南无关系。 django-mptt 的
fields
模块在 0.5.pre 之前不存在。因此,当您降级到 0.4 时,您现在会正确地收到 ImportError。我无法告诉您正确使用的导入是什么,因为我没有运行 0.4,并且由于某些疯狂的原因,开发人员没有维护 0.4 文档。但是,您的第二种方法似乎最合适。您应该没有任何理由需要在迁移中实际使用 TreeForeignKey 。它只是标准外键的包装。
This really has nothing to do with South. the
fields
module of django-mptt didn't exist before 0.5.pre. So when you downgraded to 0.4, you're now rightly getting an ImportError.I can't tell you what the proper import to use is because I'm not running 0.4 and for some crazy reason the developers did not maintain the 0.4 docs. However, your second approach seems most appropriate. There shouldn't be any reason you need to actually use TreeForeignKey in migration. It's simply a wrapper around a standard ForeignKey.