Search code examples
pythondjangodjango-simple-history

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


Scenario:

I am using the simple_history package in Django.

In the documentation, in advanced usage part the "History for a Third-Party Model" it says,

" 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"

So I put that code in the models.py (and tried admin.py too) as below:

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

register(User)

Problem: When I run python manage.py makemigrations it gives the following error:

E:! Project\CMIT\CMITv0101\cmit001>python manage.py makemigrations
Migrations for 'auth':
C:\Program Files\Python36\lib\site-packages\django\contrib\auth\migrations\0009_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\0009_histo
ricaluser.py'

What can I do now to register my User model?


Solution

  • As per the error description, you do not have permission to make changes in the folder C:\Program Files\Python36\lib\site-packages\django\contrib\auth\migrations

    Change the permission of the folder to read, write and execute. Once done, runpython manage.py makemigrations

    Also, you are trying to make migrations in system packages. You can create custom User model in a separate app and then make migrations there.