1

I have two apps 'user' and 'game' and here is my user/models.py:

from django.db import models
from django.db.models.fields import related
from django.contrib.auth.models import AbstractBaseUser , User, 
 AbstractUser , PermissionsMixin
from .managers import CustomUserManager
from game.models import Game


class User(AbstractBaseUser,PermissionsMixin):
    username = models.CharField(max_length=150, 
                unique=True, blank=True , null=True,)
    email = models.EmailField( blank=True , null=True , unique=True)
    password = models.CharField(max_length=100, null=True , blank=True)


    objects = CustomUserManager()

    class Meta:
            db_table = 'user'


class Team(models.Model):
    name = models.CharField(max_length=40, unique=True)
    players = models.ManyToManyField(User, 
           related_name="players")
    game = models.ManyToManyField(Game)
    is_verfied = models.BooleanField(default=False)

    class Meta:
        db_table = 'team'

and here is my game/models.py:

from django.db import models
from user.models import User, Team
from leaga.settings import AUTH_USER_MODEL

class Game(models.Model):
    name = models.CharField(max_length=50)
    is_multiplayer = models.BooleanField(default=False)

class Event(models.Model):
    title = models.CharField(max_length=100)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()


class Tournament(models.Model):
    title = models.CharField(max_length=50)
    is_team = models.BooleanField(default=False)
    price = models.PositiveIntegerField()
    info = models.TextField(max_length=1000)
    user = models.ManyToManyField(AUTH_USER_MODEL,
    related_name='tounament_user')
    team = models.ManyToManyField(Team, related_name='team')
    game = models.ForeignKey(Game, on_delete=models.CASCADE, related_name='tournament_game')
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='tournament_event')

    # some other code and classes here but unnecessary to mention

. . . . when i run :

 python manage.py makemigrations user

this is the console log:

 Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/Users/amir/Desktop/leaga/.venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/amir/Desktop/leaga/leaga/user/models.py", line 6, in <module>
from game.models import Game
File "/Users/amir/Desktop/leaga/leaga/game/models.py", line 3, in <module>
from user.models import Team
ImportError: cannot import name 'Team' from 'user.models' (/Users/amir/Desktop/leaga/leaga/user/models.py)

I tried to delete all .pyc files from my project

also, drop the entire database and create again(i know it's probably not the point but I got angry and I dropped the entire database and recreate it

4
  • Your issue maybe be because you're importing from both files from one to another. Try to import your package only before its use. Commented Oct 24, 2019 at 12:32
  • It looks like you defined a circular import. Commented Oct 24, 2019 at 12:36
  • @Willem Van Onsem can you please explain more what should I do now? what is the exact place of the problem? Commented Oct 24, 2019 at 12:37
  • For me it was because gunicorn didn't get reloaded on the production server. Commented Nov 24, 2020 at 6:07

1 Answer 1

5

File "/Users/amir/Desktop/leaga/leaga/user/models.py", line 6, in

from game.models import Game

File "/Users/amir/Desktop/leaga/leaga/game/models.py", line 3, in

from user.models import Team

ImportError: cannot import name 'Team' from 'user.models' (/Users/amir/Desktop/leaga/leaga/user/models.py)

From the message we can see that this is due to circular imports.

user/models.py tries to import the Game model from game/models.py, and game/models.py tries to import the Team model from user/models.py.


One solution is to remove:

from game.models import Game

From user/models.py, and change:

game = models.ManyToManyField(Game)

To:

game = models.ManyToManyField('game.Game')

You might want to read: ForeignKey and ManyToManyField.

Relevant part copied here:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.