Django 如何解决此问题:现在提供一次性默认值?

Django 如何解决此问题:现在提供一次性默认值?,django,django-models,Django,Django Models,当我进行迁移时,会出现此错误 You are trying to add the field 'post_date' with 'auto_now_add=True' to userasking without a default; the database needs something to populate existing rows. 1) Provide a one-off default now (will be set on all existing rows) 2) Qu

当我进行迁移时,会出现此错误

You are trying to add the field 'post_date' with 'auto_now_add=True' to userasking without a default; the database needs something
 to populate existing rows.

 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:
在这段代码中,我试图设置日期字段,但发现了这个错误。当我搜索此问题时,我发现我需要设置默认值,并设置:

from django.utils import timezone
post_date = models.DateField(auto_now_add=True, default=timezone.now())
设置默认值时,会出现以下错误:

WARNINGS:
community.UserAsking.post_date: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If y
ou want to have the current date as default, use `django.utils.timezone.now`
如何跳过此问题并成功迁移

models.py

class UserAsking(models.Model):
    userprofile = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    title = models.CharField(max_length=100, blank=False, help_text='Be specific and imagine you’re asking a question to another person')
    question = models.TextField(max_length=500, blank=False, help_text='Include all the information someone would need to answer your question')
    field = models.CharField(max_length=20, choices=CHOICE, default='Technology', help_text='Add the field to describe what your question is about')
    post_date = models.DateField(auto_now_add=True)

错误是,由于您现在希望日期字段有一个默认值,因此需要为已经存储在DB中的记录提供on

首先,您需要做的是改变这一点:

models.DateField(auto_now_add=True, default=timezone.now())
为此:

models.DateField(auto_now_add=True)
下一步-按照Arakkal Abu的建议执行:

选择选项1并键入timezone.now并按enter键


注意:您不需要调用像
now()
insead这样的方法,只需使用
now
(不带括号)

这是否回答了您的问题?不,由于此错误,我无法进行迁移。你读过这篇文章吗?读一下被接受的答案。它演示了如何摆脱IssueChose选项1并键入
timezone.now
并点击enteruse
timezone.now
(无妄想)not
timezone.now()
我得到一个新错误:ERRORS:community.UserAsking.post\u date:(fields.E160)选项auto\u now\u add和default是互斥的。这些选项中只能有一个。@abdelhamedabdin是的,很抱歉,您不需要定义
默认属性
,因为您已经定义了
auto\u now\u add
。我编辑了答案。如果我删除了默认值,我将得到一个错误,我应该添加一个默认值。因此,在这种情况下,如果您在迁移过程中遇到下一个错误,我可以给出什么默认值-键入
使用时区。现在将
设置为默认值,或者只设置一个默认值,如
2020-02-17
我在注释中提到的相同错误再次出现。此时,我将尝试删除数据库并再次尝试迁移