Home:ALL Converter>History of a ManyToManyField in django-simple-history

History of a ManyToManyField in django-simple-history

Ask Time:2014-06-14T00:08:00         Author:EricPb

Json Formatter

Story short, I need to save in the history the changes made on a Many-To-Many fields of one of my models.

I can see from: https://github.com/Kyruus/django-simple-history/commit/5ba8d2b4d72819f154a11f297796e6a2bb7172bf that the M2M is supported. However whenever I make a change on a M2M field it changes as well in all the history, as if never has been changed. I'm new to django and python so maybe I'm missing something.

My 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()

My 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)

I installed the django-simple-history by:

>pip install django-simple-history

Author:EricPb,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24209655/history-of-a-manytomanyfield-in-django-simple-history
ShacharSh :

This is an old open question, and might be irrelevant by now, but from looking into the code of the project in question, it seems like the last line on the ClassRoom model should be changed to\n\nhistory = HistoricalRecords(m2m_fields=['students'])\n",
2015-04-28T21:10:30
yy