Django-Simple-History: Avoid Redundant History Records & Track Generic Relations

We are using django-simple-history to track changes in our models, including many-to-many (m2m) relationships and generic relationships.

Model:

class BandProfile(BaseModel):

    class Meta:
        db_table = "band_profiles"

    name = models.CharField(max_length=255)
    types = models.ManyToManyField(
        Choices,
        related_name="types",
        limit_choices_to={"category": "type"},
        blank=True,
    )
    description = models.TextField(null=True, blank=True)

    history = HistoricalRecords(
        table_name="history_band_profiles",
        history_user_setter=set_historical_user_from_request,
        history_user_getter=get_historical_user_from_request,
        history_user_id_field=models.CharField(max_length=36, null=True),
        m2m_fields=[types],  # Many-to-many tracking
    )

    def __str__(self):
        return f"Band Profile for {self.name}"

Serializer:

    def create(self, validated_data):
        """
        Create method for BandProfile with handling for related fields.
        """
        types = validated_data.pop("type_ids", [])

        band_profile = BandProfile.objects.create(**validated_data)
        band_profile.types.set(types)

Issues:

When creating or updating resources, django-simple-history is generating multiple redundant history entries, which results in incorrect historical tracking.

Below shows the redundant records.

See the redundant records

Expected Response Format: We need a structured response that includes the historical records of the resource, its many-to-many relations, and generic relations, like this (Field names are examples):

[
    {
        "id": 101,
        "name": "Resource A",
        "history_date": "2025-03-20T10:00:00Z",
        "change_type": "update",
        "types": [
            {
                "id": 201,
                "name": "Sector X",
                "history_date": "2025-03-19T15:00:00Z",
                "change_type": "create"
            },
            {
                "id": 202,
                "name": "Sector Y",
                "history_date": "2025-03-20T16:00:00Z",
                "change_type": "update"
            }
        ],
        "generic_relation": [
            {
                "id": 301,
                "type": "Document",
                "title": "Policy Document A",
                "history_date": "2025-03-18T11:00:00Z",
                "change_type": "create"
            },
            {
                "id": 302,
                "type": "Document",
                "title": "Policy Document B",
                "history_date": "2025-03-20T14:00:00Z",
                "change_type": "delete"
            }
        ]
    }
]

Questions:

How can we prevent redundant history records when creating and updating with many-to-many relationships? Are we missing something in our configuration?

Notes:

  • We tried using a through model for many-to-many relationships, but the history records do not maintain a link between the main model and the related objects

This is a known issue with django-simple-history. It is documented here. You need to run the clean_duplicate_history management command regularly, for example with a cron job, in order to clean up those redundant history records.

Back to Top