Answer the question
In order to leave comments, you need to log in
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):
Answer the question
In order to leave comments, you need to log in
Most likely - cyclic import. In a file, you import another file and vice versa. The disease is frequent
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)
.....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question