Search code examples
pythondjangodjango-modelsdjango-south

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


Solution

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