Django App Dependency Cycle

前端 未结 4 1446
耶瑟儿~
耶瑟儿~ 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 01:47

    This may not be well-suited to your situation, but ignoring the Django aspect to your question, the general technique for breaking circular dependencies is to break out one of the cross-referenced items into a new module. For example:

    moduleA: class1, class2
               |        ^
               v        |
    moduleB: class3, class4
    

    could become:

    moduleC: class 3
               ^
               |
    moduleA: class 1, class 2
                         ^
                         |
    moduleB:          class 4
    

    (Or alternatively, you could have broken class 2 out into its own module. Or both!)

    Of course, this is no help if class A & B depend on each other. In that case, maybe they should be in the same module, or better still, maybe some part of these classes could be broken out into a third module, which both classes depend upon.

提交回复
热议问题