我收到以下OperationError当我尝试添加一个新的职位使用我的超级用户在管理网站。
OperationalError at /admin/blog/post/add/
table blog_post has no column named author_id
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/blog/post/add/
Django Version: 4.2.1
Exception Type: OperationalError
Exception Value:
table blog_post has no column named author_id
我肯定我在我的www.example.com中做错了什么models.py这是我为这个表创建的类
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
title = models.CharField(max_length = 250)
slug = models.SlugField(max_length=250)
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2,
choices=Status.choices,
default=Status.DRAFT)
class Meta:
ordering = ['-publish']
indexes = [
models.Index(fields=['-publish']),
]
def __str__(self) -> str:
return self.title
我只是设置了admin.py和settings.py来识别这个类,数据库迁移完成了,但是错误仍然存在。
迁移文件:
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=250)),
('slug', models.SlugField(max_length=250)),
('body', models.TextField()),
('publish', models.DateTimeField(default=django.utils.timezone.now)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('status', models.CharField(choices=[('DF', 'Draft'), ('PB', 'Published')], default='DF', max_length=2)),
],
options={
'ordering': ['-publish'],
'indexes': [models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx')],
},
),
]
以下是python的结果manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
blog
[X] 0001_initial
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
1条答案
按热度按时间yruzcnhs1#
通常情况下,您不会使用'author_id'访问该列,而是使用'authur.id'访问。Django在幕后做了很多操作。它必须将表转换为类,并且类约定使用句点而不是下划线。如果这不起作用,你能提供你试图访问列的代码吗?