title: Django Simple History - Tracking M2M field changes (with "through")
body: <p>I am using django-simple-history to track changes to my fields, and from what I see, it seems like django-simple-history does not track changes to M2M fields. I tried introducing an intermediate model for the M2M fields, and then attaching a HistoricalRecords() field within that intermediate model, but it seems like there is no use for that, unless I am using the django-simple-history wrongly. It seems like whenever I add a new relation in the M2M field (with "through"), a new instance is created within the intermediate model referencing foreign key relations to the two models with M2M relations, and whenever a relation is removed, that corresponding instance in the intermediate model would be deleted. If that is the case, how can I track the changes of the M2M fields through the intermediate model? Since the instance would be deleted whenever it is removed as a relation, which would lead to the tracking to not exist, since that instance no longer exists. I am new to django-simple-history, so do guide me along if I missed out or misunderstood anything, thanks all!</p>
  
  <p>Here is my code:</p>
  
  <p>models.py</p>
  
  <pre><code>class CustomerInformation(models.Model):
      customer_id = models.AutoField(primary_key=True)
      customer_name = models.CharField(max_length=100)
      history = HistoricalRecords()
  
  class SalesProject(models.Model):
      sales_project_id = models.AutoField(primary_key=True)
      sales_project_name = models.CharField(max_length=100)
      customer_information = models.ManyToManyField('CustomerInformation', through='Project_Customer')
      history = HistoricalRecords()
  
  class Project_Customer(models.Model):
      project = models.ForeignKey('SalesProject', on_delete=models.SET_NULL, null=True)
      customer = models.ForeignKey('CustomerInformation', on_delete=models.SET_NULL, null=True)
      history = HistoricalRecords()
  </code></pre>
  
  <p>serializers.py</p>
  
  <pre><code>class ProjectCustomerSerializer(serializers.ModelSerializer):
      class Meta:
          model = ProjectCustomer
          fields = ('id')
  
  class SalesProjectSerializer(serializers.ModelSerializer):
      customer_information = serializers.PrimaryKeyRelatedField(many=True, queryset=CustomerInformation.objects.all())
  
      class Meta:
          model = SalesProject
          fields = '__all__'
  
      def create(self, validated_data):
          customers = validated_data.pop('customer_information')
          project = SalesProject.objects.create(**validated_data)
          for customer in customers:
              project.customer_information.add(customer)
          return project
  
      def update(self, instance, validated_data):
          customers = validated_data.pop('customer_information')
          instance = super().update(instance, validated_data)
          instance.customer_information.clear()
          for customer in customers:
              instance.customer_information.add(customer)
          return instance
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>class SalesProjectViewSet(viewsets.ModelViewSet):
       serializer_class = SalesProjectSerializer
       queryset = SalesProject.objects.all()
  </code></pre>
  
  <p>Am I doing anything wrong here? I am not too sure how I am supposed to handle the intermediate model's creation and update of relations together with the base model (SalesProject), so I handled it manually in the create and update methods of the ModelSerializer of SalesProject, so that when I create and update a SalesProject instance, I am able to update and create the M2M relations within the intermediate model (ProjectCustomer), all in 1 call. I am not sure if the way I did is correct, especially the update method, as I am clearing all the existing relations to add new ones, so maybe that is where the issue arises from, but I am not sure what the correct way to handle this is, so do guide me along!</p>
  
  <p>All help is appreciated, thanks all!</p>