[Django]-Importing from views to models in Django

2👍

That is circular import error you are encountering. While it is bad practice to import from views to models in Django, if you still want to, you can follow methods in this question to resolve it.

Here are few alternative ways that can be considered as good practice:

  • You can consider importing other low-level modules exist within anotherapp instead of MyClass (not depending on views.py)
  • You can use Django’s signals to catch anotherapp‘s events project wide and act according to.
  • You can create a third file, say utils.py in anotherapp, move MyClass there and let anotherapp/views.py and your app’s models.py import from anotherapp.utils

Leave a comment