Django:没有这样的表:users_profile
Posted
技术标签:
【中文标题】Django:没有这样的表:users_profile【英文标题】:Django: no such table: users_profile 【发布时间】:2021-08-18 00:55:14 【问题描述】:这里是 Django 菜鸟。每当我扩展基本用户模型并尝试访问或编辑新字段时,我都会收到以下错误 [... table not found]。
OperationalError at /admin/users/profile/
no such table: users_profile
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/users/profile/
Django Version: 3.1.2
Exception Type: OperationalError
Exception Value:
no such table: users_profile
Exception Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py, line 413, in execute
Python Executable: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Python Version: 3.7.7
Python Path:
['/Users/rexmarkman/Desktop/ComputerScienceIA/newproject',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Users/rexmarkman/Library/Python/3.7/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
Server time: Sun, 30 May 2021 12:52:22 +0000
我不确定为什么会发生这种情况,因为我已经运行了 makemigrations 并多次迁移。以下是我的迁移供参考
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('profile_picture', models.ImageField(default='default.jpg', upload_to='profile_pictures')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
还有我的models.py供进一步参考
from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField(default = 'default.jpg', upload_to = 'profile_pictures')
def __str__(self):
return f'self.user.username Profile'
另外值得注意的是,在尝试更改管理站点中的配置文件表时会出现此问题
如果您需要了解其他信息,请告诉我。
感谢您的帮助:)
【问题讨论】:
你在admin.py
注册你的模型了吗?
是的,我有!我的代码分为用户应用程序和包含大部分格式的“仪表板”应用程序。我已经在用户应用程序的 admin.py 中注册了模型,是否还需要在仪表板应用程序的相应 admin.py 中注册它?
【参考方案1】:
尝试从您的应用中删除migrations
文件夹和db.sqilte3
文件,然后重试-:
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
然后重试你的错误必须解决
【讨论】:
这样做并将 Django 升级到最新版本,现在完美运行,谢谢!以上是关于Django:没有这样的表:users_profile的主要内容,如果未能解决你的问题,请参考以下文章