how to fix'argument must be a string'类型错误

f0brbegy  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(446)

我有一个元组,我正试图在我的模型中用于选择字段。尽管如此,我创建的这个表没有最后一列(可选值-可以为null),我现在正试图添加它。这是我的型号代码

class SemesterData(models.Model):
  YESNOCHOICE = (
     ('Y', 'Yes'),
    ('N', 'No'),
  )
   sid = models.ForeignKey(SessionData,on_delete=models.CASCADE)
   semester_name = models.CharField(max_length=50)
   status = models.CharField(max_length=3, choices=YESNOCHOICE, null=True, default=YESNOCHOICE[1][0])

    def __str__(self):
        return self.semester_name

我的跟踪显示

C:\Users\BOLADE\Desktop\django-project\clearance>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, system
Running migrations:
  Applying system.0002_auto_20190109_1729...Traceback (most recent call las
t):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\__init__.py", line 381, in execute_
from_command_line
    utility.execute()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\base.py", line 316, in run_from_arg
v
    self.execute(*args,**cmd_options)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args,**options)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args,**kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\core\management\commands\migrate.py", line 203, in
handle
    fake_initial=fake_initial,
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, f
ake_initial=fake_initial)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_a
ll_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=
fake_initial)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\migrations\executor.py", line 244, in apply_migr
ation
    state = migration.apply(state, schema_editor)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, p
roject_state)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\migrations\operations\fields.py", line 216, in d
atabase_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\backends\base\schema.py", line 523, in alter_fie
ld
    old_db_params, new_db_params, strict)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\backends\base\schema.py", line 626, in _alter_fi
eld
    old_default = self.effective_default(old_field)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\backends\base\schema.py", line 239, in effective
_default
    return field.get_db_prep_save(default, self.connection)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_
prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=Fa
lse)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\models\fields\__init__.py", line 785, in get_db_
prep_value
    value = self.get_prep_value(value)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\l
ib\site-packages\django\db\models\fields\__init__.py", line 1807, in get_pr
ep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number
, not 'tuple'

我将MySQL5与Django2.1和Python3.6一起使用。我在这个平台上尝试了以前的建议,但仍然显示出相同的错误。
失败的迁移


# Generated by Django 2.1.4 on 2019-01-09 16:29

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('system', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='semesterdata',
            name='status',
            field=models.CharField(choices=[('Y', 'Yes'), ('N', 'No')], default='N', max_length=3),
        ),
    ]

django 2.1.4于2019-01-08 20:00生成的0001_initial.py

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

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='DepartmentData',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('dept_name', models.CharField(max_length=50)),
                ('created_on', models.DateTimeField(auto_now_add=True)),
            ],
        ),
        migrations.CreateModel(
            name='FacultyData',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('faculty_name', models.CharField(max_length=30)),
                ('created_on', models.DateTimeField(auto_now_add=True)),
            ],
        ),
        migrations.CreateModel(
            name='SemesterData',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('semester_name', models.CharField(max_length=50)),
                ('status', models.IntegerField(choices=[('0', 'Yes'), ('1', 'No')], default=('1', 'No'))),
            ],
        ),
        migrations.CreateModel(
            name='SessionData',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('session_name', models.CharField(max_length=15)),
            ],
        ),
        migrations.AddField(
            model_name='semesterdata',
            name='sid',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='system.SessionData'),
        ),
        migrations.AddField(
            model_name='departmentdata',
            name='fid',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='system.FacultyData'),
        ),
    ]
col17t5w

col17t5w1#

初始迁移中的默认值不正确。

default=('1', 'No')

把它改成

default=1

请注意,您需要进行数据迁移才能更改任何现有的 1 以及 0 数据库中的值 'Y' 或“n”。
如果您刚刚开始项目,并且数据库中没有任何所需的数据,您可能会发现删除数据库、删除两个迁移,然后重新运行makemigrations和migrate是最简单的方法。

ffvjumwh

ffvjumwh2#

尝试使用模型选项。它更具可读性,你可以避免你的错误

from django.db import models
from model_utils import Choices

class SemesterData(models.Model):
    YESNOCHOICE = Choices(
        (0, 'yes', 'Yes'),
        (1, 'no', 'No'),
    )

    sid = models.ForeignKey(SessionData, on_delete=models.CASCADE)
    semester_name = models.CharField(max_length=50)
    status = models.IntegerField(choices=YESNOCHOICE, default=YESNOCHOICE.no)

相关问题