我对Django非常陌生。我一直在遵循一些教程来学习框架。
我在virtualenv中使用django。当我运行命令
python manage.py makemigrations
我遇到以下错误
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/Users/phyadavi/django-project/django-env/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/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/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 5, in <module>
class Game(models.Model):
File "/Users/phyadavi/django-project/django-env/tictactoe/gameplay/models.py", line 7, in Game
related_name="games_first_player")
TypeError: __init__() missing 1 required positional argument: 'on_delete'
这是模型课
from django.db import models
from django.contrib.auth.models import User
class Game(models.Model):
first_player = models.ForeignKey(User,
related_name="games_first_player")
second_player = models.ForeignKey(User,
related_name="games_second_player")
start_time = models.DateTimeField(auto_now_add=True)
last_active = models.DateTimeField(auto_now=True)
class Move(models.Model):
x = models.IntegerField()
y = models.IntegerField()
comment = models.CharField(max_length=300, blank=True)
by_first_player = models.BooleanField()
game = models.ForeignKey(Game, on_delete=models.CASCADE)
感谢您在理解和解决此问题方面的帮助。
答案 0 :(得分:2)
Starting from Django 2.0,对于所有on_delete
字段,ForeignKey
参数是必需的。您需要更改第一名和第二名玩家:
class Game(models.Model):
first_player = models.ForeignKey(
User, related_name="games_first_player", on_delete=models.CASCADE
)
second_player = models.ForeignKey(
User, related_name="games_second_player", on_delete=models.CASCADE
)
start_time = models.DateTimeField(auto_now_add=True)
last_active = models.DateTimeField(auto_now=True)
答案 1 :(得分:2)
自Django 2.0起,ForeignKey字段需要两个位置参数:
阅读documentation,并在下面添加on_delete。
first_player = models.ForeignKey(User,
related_name="games_first_player")
second_player = models.ForeignKey(User,
related_name="games_second_player")