V
V
vfvnvsyevsky2015-03-23 19:15:35
Django
vfvnvsyevsky, 2015-03-23 19:15:35

How to import one model into another?

When importing: it
from userprofile.models import CustomUser
gives an error:
ImportError: cannot import name CustomUser
I am importing from the userprofile model into the items model, respectively.
Project structure (red line from where to import):
9a4d0034596a4df5909eee7eae9a388a.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Luxurious, 2015-03-26
@oshikuru

Most likely - cyclic import. In a file, you import another file and vice versa. The disease is frequent

V
Vadim Shandrinov, 2015-03-26
@suguby

I treat cyclic imports by highlighting objects that "everyone" needs in a separate module and porting them from there. Often this can be done, but sometimes you have to jump through the files for a long time, figuring out who ported whom when.
Another way to avoid cycles is to port "in place", for example, right in the body of the function that uses the bible. Cons - imports are scattered throughout the code and it is difficult to refactor when changing a module that is being ported. This does not affect performance (well, or minimally affects it), since real import-parsing occurs only the first time the import statement is executed, and then the .pyc file is used.
By the way, in Django you can use the model name string instead of the model itself, for example:

class GoalReach(MyAbstractModel, TestFactoryBoy):
    ....
    visit = ForeignKey('Visit', null=True, blank=True, related_name='goal_reached', on_delete=CASCADE)
   .....

Visit is a model from another module, it is written as 'Visit' (in quotes) when django starts, it finds it through installedapps

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question