删除应用程序后无法删除用户

can't delete user after removing app

我有一个包含许多不同应用程序的 Django 项目。我们删除了其中一个应用程序,现在当我们尝试删除用户时失败了,并抱怨该应用程序中的 table 不存在。没有对该应用程序或任何地方的 table 的引用。测试后,我发现如果我创建 table 错误就会消失。

在谷歌搜索和四处搜索之后,我认为我已经弄明白了。 django_content_type 中引用了该应用,auth_permission 中引用了 content_type_id。所以我从 auth_permission 和 auth_permission 中删除了带有 content_type_id 的行,但我仍然收到错误。

我需要做什么才能删除对此应用的引用以便删除用户?

这是完整的堆栈跟踪:

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/options.py" in wrapper
  372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/sites.py" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/options.py" in changelist_view
  1205.                 response = self.response_action(request, queryset=cl.get_query_set(request))
File "/usr/lib/python2.6/site-packages/django/contrib/admin/options.py" in response_action
  960.             response = func(self, request, queryset)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/actions.py" in delete_selected
  35.         queryset, opts, request.user, modeladmin.admin_site, using)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/util.py" in get_deleted_objects
  109.     collector.collect(objs)
File "/usr/lib/python2.6/site-packages/django/contrib/admin/util.py" in collect
  160.             return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/deletion.py" in collect
  225.                 elif sub_objs:
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in __nonzero__
  141.         return type(self).__bool__(self)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in __bool__
  135.             next(iter(self))
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in _result_iter
  123.                 self._fill_cache()
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in _fill_cache
  927.                     self._result_cache.append(next(self._iter))
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in iterator
  301.         for row in compiler.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in results_iter
  775.         for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.         cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py" in execute
  41.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py" in execute
  130.             six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py" in execute
  120.             return self.cursor.execute(query, args)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py" in execute
  173.             self.errorhandler(self, exc, value)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: DatabaseError at /admin/auth/user/
Exception Value: (1146, "Table 'motor.reflection_resource_listeners' doesn't exist")

要从 Django 中完全删除应用程序,您需要执行以下步骤:

  1. 从所有其他应用程序和模型中删除对该应用程序的任何引用,进行迁移并应用它们。 (这将删除您要删除的模型的所有外键关系)

  2. 注释掉您要删除的应用程序的所有模型,然后进行迁移并应用它们。 (这将删除您要删除的模型的所有表)

  3. 在其他应用中的所有引用都被删除并且应用本身没有模型后,您可以删除整个应用的文件。

  4. 最后,您需要删除在初始迁移时创建的所有 ContentType 条目 运行。您可以通过 运行 在此处找到的脚本执行此操作:Remove Content Types