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

Problems with accessing a Model in the Admin website

我已经创建了 Profile 模型,它在 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']


这就是问题开始的地方,我可以成功地看到 Clients 模型是在管理部分创建的,但是当我单击该模型时 - 我得到了这个错误 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

欢迎提出有关如何解决此问题的任何想法

尝试运行

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
  1. 手动检查数据库中是否存在table
  2. 如果 table 不存在,请在数据库中手动创建 table。
  • 选项 2
  1. 如果它的调试数据库没有重要信息删除它,再删除migrations和运行 migrations。

堆栈跟踪说明了一切,您的应用似乎无法找到 table account_client。当您进行迁移时,一定是出了什么问题。如果可能的话,从一个新的数据库开始或者至少删除所有 tables。然后删除所有迁移文件并重做 makemigrations 命令。之后,您就可以开始了!祝你好运!