我已经设置了所有内容来迁移posgresql而不是sqlite。我已经完成了数个教程中的所有操作,还安装了psycopg2,但无法迁移模型。 Settings.py数据库如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'blog',
'USER': 'postgres',
'PASSWORD': 'xxxxxxx',
'HOST': 'localhost',
'PORT': '5432'
},
}
当我运行迁移时,会出现此错误:
Applying blogapp.0002_auto_20181031_1421...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\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\PZAWA\AppData\Local\Programs\Python\Python37\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\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\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\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\postgresql\schema.py", line 122, in _alter_field
new_db_params, strict,
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py", line 627, in _alter_field
new_default = self.effective_default(new_field)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related.py", line 937, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\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=False)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 956, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\PZAWA\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'
我不知道我该如何处理这个问题,sqlite的工作原理就像一个魅力,看起来像是对用户模型的某些问题进行了扩展。迁移文件:
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blogapp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('comment_text', models.TextField(blank=True)),
('comment_author', models.CharField(help_text='Enter your nickname.', max_length=50)),
('comment_date', models.DateTimeField(auto_now_add=True)),
],
),
migrations.AlterModelOptions(
name='blogs',
options={'verbose_name_plural': 'Blogs'},
),
migrations.RemoveField(
model_name='author',
name='name',
),
migrations.AddField(
model_name='author',
name='user',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='author', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='post',
name='image',
field=models.ImageField(default='media/default.jpg', upload_to='media'),
),
migrations.AddField(
model_name='post',
name='post_text',
field=models.TextField(blank=True),
),
migrations.AlterField(
model_name='post',
name='author',
field=models.ForeignKey(default=django.contrib.auth.models.User, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='post',
name='post_date',
field=models.DateTimeField(auto_now_add=True),
),
migrations.AddField(
model_name='comment',
name='post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blogapp.Post'),
),
]
答案 0 :(得分:0)
问题是将author
设置为author = models.ForeignKey( User, on_delete=models.SET_NULL, null=True, default=User)
的默认属性
现在一切正常。