访问管理员网站中的模型时出现问题

unguejic  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(297)

我已经创建了概要文件模型,它在django的管理网站上运行良好,显然,我尝试创建了一个非常复杂的模型,但我不断出错,无论如何,我决定简化流程,并创建了一个非常简单的模型。
models.py

from django.db import models
from django.conf import settings

# from django_countries.fields import CountryField

# from phone_field import PhoneField

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    email = models.EmailField(blank=True,null=True)
    role = models.TextField(blank=True)
    location = models.TextField(blank=True)
    photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)

    def __str__(self):
        return f'Profile for user {self.user.username}'

class Client(models.Model):
    firstname = models.CharField(blank=True, max_length=30)

    def __str__(self):
        return f'{self.firstname}'

我跑 python manage.py makemigrationspython manage.py migrate 代码运行良好,我还确保删除所有以前的迁移,因此我在迁移中成功创建了0001_initial.py文件。
我决定将我的新模型包含在admin.py文件中,这样我就可以从django的用户界面与模型交互

from django.contrib import admin
from .models import Profile, Client

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'photo','role']

@admin.register(Client)
class Client(admin.ModelAdmin):
    list_display = ['firstname']
    #list_display = ['firstname', 'lastname','position','country','email','phone']

这就是问题开始的地方,我可以成功地看到客户端模型是在管理部分创建的,但是当我点击模型时,我得到了这个错误 django.db.utils.OperationalError: no such table: account_client ```
Traceback (most recent call last):
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args,**callback_kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\options.py", line 616, in wrapper
return self.admin_site.admin_view(view)(*args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\sites.py", line 232, in inner
return view(request, *args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args,**kwargs)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\options.py", line 1697, in changelist_view
cl = self.get_changelist_instance(request)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\options.py", line 736, in get_changelist_instance
return ChangeList(
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\views\main.py", line 100, in init
self.get_results(request)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\contrib\admin\views\main.py", line 235, in get_results
result_count = paginator.count
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\utils\functional.py", line 48, in get
res = instance.dict[self.name] = self.func(instance)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\core\paginator.py", line 97, in count
return c()
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\query.py", line 412, in count
return self.query.get_count(using=self.db)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\sql\query.py", line 519, in get_count
number = obj.get_aggregation(using, ['__count'])['__count']
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\sql\query.py", line 504, in get_aggregation
result = compiler.execute_sql(SINGLE)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
cursor.execute(sql, params)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\fcolina\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: account_client

任何关于如何解决这个问题的想法都是欢迎的
lyr7nygr

lyr7nygr1#

stacktrace说明了这一切,似乎你的应用程序找不到表 account_client . 迁移时一定出了什么问题。如果可能的话,从一个新的db开始,或者至少删除所有表。然后删除所有迁移文件并重新执行迁移 makemigrations 指挥部。在那之后,你就可以走了!祝你好运

5lwkijsr

5lwkijsr2#

试着跑

python manage.py migrate --run-syncdb

运行正常迁移

python manage.py makemigrations <your-app-name>
python manage.py migrate <your-app-name>

如果不起作用,请尝试将其添加到每个模型:

class Meta:
        app_label = '<your-app-name>'

运行正常迁移

python manage.py makemigrations <your-app-name>
python manage.py migrate <your-app-name>

如果仍然存在,则:

python manage.py makemigrations <your-app-name>
python manage.py migrate <your-app-name> --fake

如果所有选项都不起作用,则有两个选项:
选择1
手动检查数据库中是否存在表
如果表不存在,请在数据库中手动创建该表。
选择2
如果它的调试数据库没有重要信息,请删除它,删除迁移并再次运行迁移。

相关问题