ValueError:字段“ id”应为数字,但得到“选择”

时间:2020-03-14 09:42:11

标签: python django django-migrations

当我运行“ python manage.py migration”命令时,发生错误:

要执行的操作: 应用所有迁移:管理员,身份验证,内容类型,会话,travello 运行迁移: 正在应用travello.0005_auto_20200313_1046 ...回溯(最近一次通话): 文件“ C:\ Users \ uc \ Envs \ test \ lib \ site-packages \ django \ db \ models \ fields__init __。py”,行1772,在get_prep_value中 返回int(值) ValueError:int()以10为底的无效文字:'select'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\backends\base\schema.py", line 564, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\backends\postgresql\schema.py", line 152, in _alter_field
    super()._alter_field(
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\backends\base\schema.py", line 679, in _alter_field
    new_default = self.effective_default(new_field)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\models\fields\related.py", line 939, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\models\fields\__init__.py", line 821, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\models\fields\__init__.py", line 2365, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\uc\Envs\test\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
    raise e.__class__(
ValueError: Field 'id' expected a number but got 'select'.

models.py

 from django.db import models

class Destination(models.Model):  
    name = models.CharField(max_length=200)
    desc = models.TextField()
    price = models.IntegerField()
    img = models.ImageField(upload_to= 'pics')
    offer = models.BooleanField(default=False)

class Category(models.Model):
    cat_name = models.CharField(max_length = 50)

    def __str__(self):
        return self.cat_name

class Blog(models.Model):
    date = models.DateField()
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default=0)
    desc = models.TextField()
    img = models.ImageField(upload_to= 'blog_img')

class Subscribe(models.Model):
    name = models.CharField(max_length=100)
    email = models.CharField(max_length=100)

admin.py

from django.contrib import admin
from .models import Destination
from .models import Blog
from .models import Category
from .models import Subscribtion
admin.autodiscover()

admin.site.register(Destination)
admin.site.register(Blog)
admin.site.register(Category)
admin.site.register(Subscribtion)

0005_auto_20200313_1046.py

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
        ('travello', '0004_auto_20200312_1224'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='blog',
            name='date',
        ),
        migrations.AddField(
            model_name='blog',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='blog',
            name='updated_at',
            field=models.DateTimeField(auto_now=True),
        ),
        migrations.AlterField(
            model_name='blog',
            name='category',
            field=models.ForeignKey(default='select', on_delete=django.db.models.deletion.CASCADE, to='travello.Category'),
        ),
    ]

0 个答案:

没有答案