django-simple-history中ManyToManyField的历史

时间:2014-06-13 16:08:00

标签: python django django-models django-admin

故事简短,我需要在历史记录中保存我的某个模型的多对多字段所做的更改。

我可以看到:https://github.com/Kyruus/django-simple-history/commit/5ba8d2b4d72819f154a11f297796e6a2bb7172bf 支持M2M。但是,每当我在M2M字段上进行更改时,它在所有历史记录中都会发生变化,就像从未更改过一样。我是django和python的新手,所以也许我错过了什么。

我的models.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()

我的admin.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)

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

>pip install django-simple-history

1 个答案:

答案 0 :(得分:4)

这是一个陈旧的问题,现在可能无关紧要,但是通过查看相关项目的代码,似乎应该将ClassRoom模型的最后一行更改为

history = HistoricalRecords(m2m_fields=['students'])