Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从models.py迁移数据时出现问题_Python_Django_Django Models_Django Admin_Django Migrations - Fatal编程技术网

Python 从models.py迁移数据时出现问题

Python 从models.py迁移数据时出现问题,python,django,django-models,django-admin,django-migrations,Python,Django,Django Models,Django Admin,Django Migrations,伙计们,我得到了一个错误,因为你们应该对模型有一个默认值:author、body、created、updated 这是我的models.py配置 from django.db import models from django.utils import timezone from django.contrib.auth.models import User # Create your models here. class post(models.Model): STATUS_CHOICE

伙计们,我得到了一个错误,因为你们应该对模型有一个默认值:author、body、created、updated 这是我的models.py配置

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class post(models.Model):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
    )

    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length = 250,unique_for_date=1)
    author=models.ForeignKey(User,related_name='blog_posts')
    body=models.TextField()
    publish=models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated=models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                            choices = STATUS_CHOICE,
                                default='draft')
    class Meta:
        ordering = ('-publish',)
    def __str__(self):
        return self.title
迁移数据库时出现以下错误:

You are trying to add a non-nullable field 'body' to post without a default; we can't do that (the database needs something to populate
existing rows).
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
Select an option
我不确定这些模型的默认值是什么,配置和书中提到的相同,但我仍然得到一个错误 感谢您的任何帮助 提前谢谢 注意:这是一个博客应用程序 当我尝试向模型添加随机默认值时,我得到了以下错误: 更新的models.py:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class post(models.Model):
    STATUS_CHOICE=(
        ('draft','DRAFT'),
        ('published','Published'),
        ('admin','admin'),
    )

    title=models.CharField(max_length=250)
    slug=models.SlugField(max_length = 250)
    author=models.ForeignKey(User,related_name='blog_posts')
    body=models.TextField(default='draft')
    publish=models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True,default=timezone.now)
    updated =models.DateTimeField(auto_now=True,default=timezone.now)
    status = models.CharField(max_length=10,
                            choices = STATUS_CHOICE,
                                default='draft')
    class Meta:
        ordering = ('-publish',)
    def __str__(self):
        return self.title
现在的错误是:

ERRORS:
myblog.post.created: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may
 be present.
myblog.post.updated: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may
 be present

为什么不使用null=True,blank=True——这允许模型属性为空

我会将body属性的默认值设置为

body=models.TextField(default='')
时间戳不应该有默认值,您提供的可调用项应该足够了

# Model Timestamp
created     = models.DateTimeField(auto_now_add=True)
updated     = models.DateTimeField(auto_now=True)
执行迁移时,如果创建和更新的属性遇到缺少默认错误,请选择1并在控制台中输入
时区,现在。将现有行的默认值设置为当前日期时间

错误看起来很清楚,您不需要DateTimeField中的两个选项谢谢您的回复是的,我已经删除了DateTimeField的值并添加了默认值,但仍然要求我提供slug和author,请您确定默认值应该是多少,以便在编辑完您所说的所有内容后查看我的问题仍然会出现此错误:您正在尝试添加字段“created”和“auto\u now\u add=True”,以在没有默认值的情况下发布;数据库需要一些东西来填充现有的行。1提供一个一次性默认值现在将在所有现有行上设置2退出,让我在models中添加一个默认值。py选择一个选项:myblog\models。py14:71我更改了答案,现在应该通过了。谢谢它工作了,我刚刚为所有这些添加了null=true,如果你想帮助像我这样的孩子解决问题,请加入这个团队: