django-simple-history 中 ManyToManyField 的历史

Eri*_*cPb 5 python django django-models django-admin

简而言之,我需要在历史记录中保存对我的模型之一的多对多字段所做的更改。

我可以从https://github.com/Kyruus/django-simple-history/commit/5ba8d2b4d72819f154a11f297796e6a2bb7172bf看到 支持M2M。然而,每当我对 M2M 领域进行更改时,它在所有历史记录中也会发生变化,就好像从未更改过一样。我是 django 和 python 的新手,所以也许我错过了一些东西。

我的模型.py:

from django.db import models
from simple_history.models import HistoricalRecords

class Student(models.Model):
  studentname = models.CharField(max_length=50, verbose_name='Name')

class ClassRoom(models.Model):
  classname = models.CharField(max_length=50, verbose_name='Name')
  students = models.ManyToManyField(Student)
  history = HistoricalRecords()
Run Code Online (Sandbox Code Playgroud)

我的管理员.py:

from django.contrib import admin
from school.models import Student, ClassRoom
from simple_history.admin import SimpleHistoryAdmin

class StudentAdmin(SimpleHistoryAdmin):
  list_display = ('studentname',)

class ClassRoomAdmin(SimpleHistoryAdmin):
  list_display = ('classname',)

admin.site.register(Student,StudentAdmin)
admin.site.register(ClassRoom, ClassRoomAdmin)
Run Code Online (Sandbox Code Playgroud)

我通过以下方式安装了 django-simple-history:

>pip install django-simple-history
Run Code Online (Sandbox Code Playgroud)

Sha*_*rSh 6

这是一个古老的开放问题,现在可能无关紧要,但从查看相关项目的代码来看,ClassRoom 模型的最后一行似乎应该更改为

history = HistoricalRecords(m2m_fields=['students'])
Run Code Online (Sandbox Code Playgroud)