ValueError:int()的基数为10的无效文字:' NULL'

时间:2017-04-01 18:14:56

标签: 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时,显示出来了:

您正在尝试添加不可为空的字段&#39;类别&#39;没有默认的文章;我们不能这样做(数据库需要一些东西来填充现有的行)。 请选择修复:

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

2)退出,让我在models.py

中添加默认值

选择一个选项:

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

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

1 个答案:

答案 0 :(得分:0)

您不应该提供您希望数据库接收的值。你在Python中给出了值。但是,如果在字段上没有设置null = True,则只会获得该消息。删除迁移并在字段上设置null = True。