Django App Dependency Cycle

前端 未结 4 1448
耶瑟儿~
耶瑟儿~ 2020-12-08 01:28

I am in the middle of developing a Django application, which has quite complicated models (it models a university - courses, modules, lectures, students etc.)

I have

4条回答
  •  情话喂你
    2020-12-08 02:03

    If your dependency is with foreign keys referencing models in other applications, you don't need to import the other model. You can use a string in your ForeignKey definition:

    class MyModel(models.Model):
        myfield = models.ForeignKey('myotherapp.MyOtherModel')
    

    This way there's no need to import MyOtherModel, so no circular reference. Django resolves the string internally, and it all works as expected.

提交回复
热议问题