Forward class declaration in Python

混江龙づ霸主 提交于 2019-12-10 14:44:33

问题


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. 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?


回答1:


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')

Update: According to Django/Python Circular model reference that's exactly the way to go.



来源:https://stackoverflow.com/questions/5308895/forward-class-declaration-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!