Solve cross-import Django models

If you have A model in a package and B model in b package sometimes you may have situation when you need import A model in b package and B model in a package. Normal solution will be reorganizing your structure in that way to have no such references but you also should know about another fast way to solve it.

You have possibility use model without importing it:

from django.apps import apps

def your_view(request): 
    YourModel = apps.get_app_config('your_app_name').get_model('YourModel ')
    YourModel.objects.filter(...)
#django
0
Ivan Borshchov profile picture
Nov 03, 2016
by Ivan Borshchov
Did it help you?
Yes !
No

Best related