【发布时间】:2020-03-28 01:43:19
【问题描述】:
我是 django 新手,在将 Django auth 模型直接集成到应用程序模型中时遇到问题,将其称为:
class Question(models.Model):
question_text = models.CharField(max_length=200, blank=False)
pup_date = models.DateField('date_published',auto_now=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
但是在尝试运行python manage.py makemigrations 时会出现以下错误
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
那么请你帮我解决这个问题,我不需要创建自定义用户模型。
问候
【问题讨论】:
-
这不是一个错误,而是 Django 的一个有用特性。您的迁移是说您正在尝试将属性(列)添加到类 Question。在没有看到完整输出的情况下,我不能肯定地说,但我猜当数据库中已有问题时,您正试图将作者 (null=False) 添加到 Question 中。因此,您可以为 Question 中的现有行提供一次性值,或者您必须更新您的模型。您可以找到您自己的用户的PK,并在迁移工具询问时输入。请显示完整的输出,我们可以提供帮助
-
实际上这是完整的消息,并且在 Question 模型中没有记录,但我认为作者是用户模型
django.contrib.auth.models.User的 ForeignKey 而作者实际上是用户模型的 PK。这就是为什么它不应该为空
标签: django authentication