未启用Django auth时,如何禁用Simple History的用户追踪功能?
问题
正在开发一个Django项目,希望使用Django Simple History记录数据变更,但遇到问题:不需要追踪执行变更的用户(未使用Django的auth应用)。执行迁移时出现如下错误:
SystemCheckError: System check identified some issues: ERRORS: my_app.HistoricalModel.history_user: (fields.E300) Field defines a relation with model 'auth.User', which is either not installed, or is abstract. data_lake.HistoricalInstance.history_user: (fields.E307) The field my_app.HistoricalInstance.history_user was declared with a lazy reference to 'auth.user', but app 'auth' isn't installed.
请问是否可以禁用用户模型关联,使用HistoricalRecords?
解决方案
完全可以禁用Django Simple History对用户的追踪,具体操作步骤如下:
在模型中定义
HistoricalRecords时,传入history_user_field=None参数,这样就不会自动生成关联auth.User的history_user字段。
示例代码:from django.db import models from simple_history.models import HistoricalRecords class YourModel(models.Model): # 自定义模型字段 name = models.CharField(max_length=100) # 禁用用户追踪 history = HistoricalRecords(history_user_field=None)若之前已生成过相关迁移文件,需先删除对应app下migrations目录中涉及Django Simple History的迁移文件,再重新执行以下命令:
python manage.py makemigrations python manage.py migrate
内容的提问来源于stack exchange,提问作者Barranka
相关产品推荐
相关产品推荐

