python - 值错误 : invalid literal for int() with base 10: 'NULL'

标签 python mysql django python-3.x

当我在我的文件上运行 python manage.py migrate 时,出现了这个 ValueError。

Operations to perform:
  Apply all migrations: admin, auth, cms, contenttypes, sessions
Running migrations:
  Applying cms.0002_auto_20170401_2307...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\sndys\project\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\sndys\project\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\sndys\project\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\sndys\project\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Users\sndys\project\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\sndys\project\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Users\sndys\project\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Users\sndys\project\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
    self.effective_default(field)
  File "C:\Users\sndys\project\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\related.py", line 909, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 938, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\sndys\project\lib\site-packages\django\db\models\fields\__init__.py", line 946, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'NULL'

我的 models.py 文件:

from django.db import models
from django.contrib.auth.models import User
import markdown

# Create your models here.
class Tag(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()

    def __str__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()
    description = models.TextField()
    image = models.ImageField()

    def __str__(self):
        return self.name

class Image(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()
    alt_text = models.CharField(max_length=255)
    image = models.ImageField()

    def __str__(self):
        return self.name

class Gallery(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()
    images = models.ManyToManyField(Image)
    description = models.TextField()
    layout = models.CharField(
        max_length=5,
        choices=(
            ('LINK', 'Linked'),
            ('HORI', 'Horizontal'),
            ('VERT', 'Vertical')))

    def __str__(self):
        return self.name

class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    author = models.ForeignKey(User)
    content = models.TextField()
    creation_date = models.DateTimeField(auto_now_add=True)
    edit_date = models.DateTimeField(auto_now=True)
    featured_image = models.ImageField(null=True, blank=True)
    publish_status = models.BooleanField(default=False)
    category = models.ForeignKey(Category)
    tags = models.ManyToManyField(Tag)

    @property
    def content_html(self):
        return markdown.markdown(self.content)
    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = 'Articles'
        ordering = ['creation_date']`enter code here`

当我执行 python manage.py makemigrations 时,出现了:

您正在尝试在没有默认值的情况下向文章添加不可为空的字段“类别”;我们不能那样做(数据库需要一些东西来填充现有行)。 请选择一个修复:

1) 现在提供一次性默认值(将在所有现有行上设置此列的空值)

2) 退出,让我在 models.py 中添加一个默认值

选择一个选项:

我选择了 1 并给出了默认值“NULL”,我认为这可能是我犯的错误。在此“python manage.py makemigrations”成功运行之后,但“python manage.py migrate”出现错误。 在此之后,我尝试为 Article 类中的类别外键提供默认值 = 1,但迁移时出现相同的错误。

谁能告诉我问题出在哪里以及如何解决。

最佳答案

您不应该提供希望数据库接收的值。您在 Python 中给出值。但是,只有在该字段上未设置 null=True 时,您才会收到该消息。删除迁移并在该字段上设置 null=True。

关于python - 值错误 : invalid literal for int() with base 10: 'NULL' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160365/

相关文章:

Python方式从文件中导出数字证书信息

python - ValueError : logits and labels must have the same shape ((None, 14)与(无,1))

python - Owner_id 不能为空。 DRF关系数据库

PHP、MySQL、字符显示不正确。

python - 考虑基线值,根据具体条件减去和添加值

python - 使用 Python 检查数据库时循环遍历目录中的文件时出现问题

java - 执行 JDBC 事务时出现重复键异常

python - Django 清除所有管理员列表过滤器

django - Django 应用程序的负载测试

django - 何时在 Django 中使用 HttpResponseBadRequest (http 400)