Django models django-在传递_delete参数后获取错误

Django models django-在传递_delete参数后获取错误,django-models,Django Models,在django 2.0中创建一个游戏模型,同时在外键中传递on_deletearg 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_pla

在django 2.0中创建一个游戏模型,同时在外键中传递
on_delete
arg

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)
您忘记了在您的
游戏
模型中指定您的
外键
s的名称:

from django.contrib.auth import get_user_model

class Game(models.Model):
    first_player = models.ForeignKey(
        get_user_model(),
        related_name='games_first_player',
        on_delete=models.CASCADE
    )
    second_player = models.ForeignKey(
        get_user_model(),
        related_name='games_second_player'
        on_delete=models.CASCADE
    )
    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)
从django.contrib.auth导入get\u user\u模型
班级游戏(models.Model):
第一个玩家=models.ForeignKey(
获取用户模型(),
相关的“游戏第一玩家”,
on_delete=models.CASCADE
)
第二个玩家=models.ForeignKey(
获取用户模型(),
相关的\u name='games\u second\u player'
on_delete=models.CASCADE
)
start\u time=models.datetime字段(auto\u now\u add=True)
last\u active=models.datetime字段(auto\u now=True)
您可能希望在
用户
上使用,因为如果以后更改您的用户模型,您可以轻松更新所有
外键
s。

您忘记了在
游戏
模型中指定
外键
s的位置:

from django.contrib.auth import get_user_model

class Game(models.Model):
    first_player = models.ForeignKey(
        get_user_model(),
        related_name='games_first_player',
        on_delete=models.CASCADE
    )
    second_player = models.ForeignKey(
        get_user_model(),
        related_name='games_second_player'
        on_delete=models.CASCADE
    )
    start_time = models.DateTimeField(auto_now_add=True)
    last_active = models.DateTimeField(auto_now=True)
从django.contrib.auth导入get\u user\u模型
班级游戏(models.Model):
第一个玩家=models.ForeignKey(
获取用户模型(),
相关的“游戏第一玩家”,
on_delete=models.CASCADE
)
第二个玩家=models.ForeignKey(
获取用户模型(),
相关的\u name='games\u second\u player'
on_delete=models.CASCADE
)
start\u time=models.datetime字段(auto\u now\u add=True)
last\u active=models.datetime字段(auto\u now=True)

您可能希望使用over
User
,因为如果以后更改用户模型,你可以很容易地更新所有的
ForeignKey
s.

你会遇到什么错误?你忘了为
第一个玩家
第二个玩家
指定
。你会遇到什么错误?你忘了为
第一个玩家
第二个玩家
指定
。谢谢,威廉,我现在知道了!!谢谢,威廉,我现在拿到了!!