在Python中转发类声明 [英] Forward class declaration in Python
问题描述
我有两个课程:
class A(models):
...
class B(models):
a = models.ManyToManyField(A)
现在我必须将我的模型更改为以下:
Now I have to change my model to one below:
class A(models):
b = models.ManyToManyField(B)
class B(models):
...
我必须使用南移。我想在A类中创建新的多对多字段,从B类迁移数据和删除字段。问题是两者都在同一模型中。所以当我把很多到很多的A类,这是不能被看到的。因为B声明低于A.如何解决这个问题?
I have to use south migrations. I wanted to create new many to many field in class A, migrate data and delete field from class B. The problem is that both are in same model. So when I put many to many into A class it cannot be seen. Because B declaration is below A. How to solve this problem?
推荐答案
至少SQLAlchemy允许您使用字符串而不是类。尝试如果django-orm也允许。
At least SQLAlchemy allows you to use a string instead of a class. Try if django-orm allows that, too.
a = models.ManyToManyField('A')
# ...
b = models.ManyToManyField('B')
更新:根据 Django / Python循环模型参考完全可以走。
Update: According to Django/Python Circular model reference that's exactly the way to go.
这篇关于在Python中转发类声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!