Django:在 'simple_history' 包中注册 'User' 模型时遇到问题

Django : Trouble facing registering 'User' model in the 'simple_history' package

场景:

我在 Django 中使用 simple_history 包。

在文档的高级用法部分,“History for a Third-Party Model”表示,

" To track history for a model you didn’t create, use the simple_history.register utility. You can use this to track models from third-party apps you don’t have control over. Here’s an example of using simple_history.register to history-track the User model from the django.contrib.auth app"

所以我将该代码放在 models.py 中(也尝试了 admin.py),如下所示:

from simple_history import register
from django.contrib.auth.models import User

register(User)

问题: 当我 运行 python manage.py makemigrations 它给出以下错误:

E:! Project\CMIT\CMITv0101\cmit001>python manage.py makemigrations
Migrations for 'auth':
C:\Program Files\Python36\lib\site-packages\django\contrib\auth\migrations[=13=]09_historicaluser.py
- Create model HistoricalUser
Traceback (most recent call last):
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management_init_.py", line 363, in execute_from_command_line
utility.execute()
File "C:\Program Files\Python36\lib\site-packages\django\core\management_init_.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\commands\makemigrations.py", line 193, in handle
self.write_migration_files(changes)
File "C:\Program Files\Python36\lib\site-packages\django\core\management\commands\makemigrations.py", line 232, in write_migration_files
with io.open(writer.path, "w", encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: 'C:\Program Files\Python36\lib\site-packages\django\contrib\auth\migrations[=13=]09_histo
ricaluser.py'

我现在可以做什么来注册我的用户模型?

根据错误描述,您无权更改文件夹 C:\Program Files\Python36\lib\site-packages\django\contrib\auth\migrations

更改文件夹的读取、写入和执行权限。一旦完成,运行python manage.py makemigrations

此外,您正在尝试在系统包中进行迁移。您可以在单独的应用程序中创建自定义用户模型,然后在那里进行迁移。