Skip to content

update_change_reason: fails to fetch latest history entry for nullable fields #1475

Closed
@Senor-Ducky

Description

@Senor-Ducky

Describe the bug
The update_change_reason method in utils.py fails to update change reason in case any field is nullable in a model or is set to default to None. This could be due to the way django handles null values, as direct comparison to None is not allowed when filtering.

To Reproduce
Steps to reproduce the behavior:

  1. Create a model with any nullable field or a field that defaults to None and enable historical records.
  2. Try to use the update_change_reason on an instance of the model.

Expected behavior
The update_change_reason should handle models with nullable fields/fields with default set to None gracefully by applying the correct filter query for fetching historical records.

Screenshots
Image

Environment (please complete the following information):

  • OS: Win 11 24H2
  • Django Simple History Version: 3.8.0
  • Django Version: 5.0.11
  • Database Version: PostgreSQL 16.8

Additional context
In the provided screenshot you can see, the History Attrs logged from within the update_change_reason method indicating that it is using the wrong filter query for filtering/matching null value, instead of field=None it should have been field__isnull=True, which prevented the historical record from showing up causing an exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions