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

We are using django-simple-history to track changes in our models, including many-to-many (m2m) relatio

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

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
Share Improve this question asked Mar 25 at 8:46 user30040602user30040602 511 silver badge5 bronze badges 2
  • Are u sure this is not because Multiple save operations may occurring during a single transaction? – Bhargav Commented Mar 25 at 11:40
  • It looks like the redundant record creation is related to the use of the set method. When I remove it, the issue doesn’t occur. However, I’m not sure what the correct approach would be to fix this. – user30040602 Commented Mar 26 at 6:48
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744209054a4563246.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信