简体   繁体   English

Python中的前向类声明

[英]Forward class declaration in Python

I have two classes in order: 我按顺序有两个班:

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):
    ...

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. 我想在A类中创建新的多对多字段,从B类迁移数据和删除字段。问题是两者都在同一个模型中。 So when I put many to many into A class it cannot be seen. 因此,当我将多对多的东西放入A类时,它是无法看到的。 Because B declaration is below A. How to solve this problem? 因为B声明低于A.如何解决这个问题?

At least SQLAlchemy allows you to use a string instead of a class. 至少SQLAlchemy允许您使用字符串而不是类。 Try if django-orm allows that, too. 尝试django-orm也允许这样做。

a = models.ManyToManyField('A')
# ...
b = models.ManyToManyField('B')

Update: According to Django/Python Circular model reference that's exactly the way to go. 更新:根据Django / Python循环模型参考 ,这正是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM