I want to create a back-end server for an online video website with Django. There are user information and video information in the database. I want users to pay attention to each other and collect videos. Users can also upload videos. For this
I registered two apps: user and video. And added to INSTALLED_APPS. After I writing models.py, I run python manage.py makemigrations
and reported error:
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\Code\PycharmProjects\djangoProject_dachuang\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "D:\Code\PycharmProjects\djangoProject_dachuang\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "D:\Code\PycharmProjects\djangoProject_dachuang\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Code\PycharmProjects\djangoProject_dachuang\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\Code\PycharmProjects\djangoProject_dachuang\venv\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "D:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\Code\PycharmProjects\djangoProject_dachuang\user\models.py", line 8, in <module>
from video.models import Video
File "D:\Code\PycharmProjects\djangoProject_dachuang\video\models.py", line 20, in <module>
from user.models import UserProfile
ImportError: cannot import name 'UserProfile'
this is user->models.py:
from django.db import models
from video.models import Video
class UserProfile(models.Model):
openid = models.CharField(max_length=50, unique=True, default='')
nickname = models.CharField(max_length=50, default='')
avatar = models.CharField(max_length=255, default='')
liked_videos = models.ManyToManyField('Video',on_delete=models.CASCADE)
fans = models.ManyToManyField('self', symmetrical=False)
liked_users = models.ManyToManyField('self', symmetrical=False)
created_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now_add=True)
this is video->models.py:
from django.db import models
from user.models import UserProfile
class Video(models.Model):
STATUS_CHOICES = (
('0', 'can'),
('1', 'can not'),
)
title = models.CharField(max_length=100, blank=True, null=True)
desc = models.CharField(max_length=255, blank=True, null=True)
classification = models.CharField(max_length=100, blank=True, null=True)
file = models.FileField(max_length=255, upload_to='videos', blank=True, null=True)
cover = models.CharField(max_length=255, blank=True, null=True)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, blank=True, null=True)
created_user = models.ForeignKey('UserProfile', max_length=255, blank=True, null=True, on_delete=models.CASCADE)
create_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now_add=True)
CodePudding user response:
The issue is caused by circular imports. Please note the changes in users/models.py
.
from django.db import models
class UserProfile(models.Model):
openid = models.CharField(max_length=50, unique=True, default='')
nickname = models.CharField(max_length=50, default='')
avatar = models.CharField(max_length=255, default='')
liked_videos = models.ManyToManyField('video.Video',on_delete=models.CASCADE)
fans = models.ManyToManyField('self', symmetrical=False)
liked_users = models.ManyToManyField('self', symmetrical=False)
created_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now_add=True)
and in video/models.py
from django.db import models
class Video(models.Model):
STATUS_CHOICES = (
('0', 'can'),
('1', 'can not'),
)
title = models.CharField(max_length=100, blank=True, null=True)
desc = models.CharField(max_length=255, blank=True, null=True)
classification = models.CharField(max_length=100, blank=True, null=True)
file = models.FileField(max_length=255, upload_to='videos', blank=True, null=True)
cover = models.CharField(max_length=255, blank=True, null=True)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, blank=True, null=True)
created_user = models.ForeignKey('users.UserProfile', max_length=255, blank=True, null=True, on_delete=models.CASCADE)
create_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now_add=True)