Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support customizing the history manager and historical queryset classes #1306

Merged
merged 9 commits into from
Feb 24, 2024

Conversation

tim-schilling
Copy link
Contributor

@tim-schilling tim-schilling commented Feb 13, 2024

This allows customizing the history model's Manager and QuerySet classes.

Description

To manipulate the History Manager or the Historical QuerySet, you can specify the history_manager and historical_queryset options. Tht values must be subclasses of simple_history.manager.HistoryManager and simple_history.manager.HistoricalQuerySet respectively.

Example:

from datetime import timedelta
from django.db import models
from django.utils import timezone
from simple_history.models import HistoricalRecords
from simple_history.manager import HistoryManager, HistoricalQuerySet

class HistoryQuestionManager(HistoryManager):
    def published(self):
        return self.filter(pub_date__lte=timezone.now())

class HistoryQuestionQuerySet(HistoricalQuerySet):
    def question_prefixed(self):
        return self.filter(question__startswith='Question: ')

class Question(models.Model):
    pub_date = models.DateTimeField('date published')
    history = HistoricalRecords(
        history_manager=HistoryQuestionManager,
        historical_queryset=HistoryQuestionQuerySet,
    )

Related Issue

Closes #1280
Probably closes #963

Motivation and Context

I have a multi-tenant application and I have a custom QuerySet class that I wanted to re-use.

How Has This Been Tested?

I've tested this with my application, though not in production yet. I also added tests to the test suite that cover the cases adequately.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the pre-commit run command to format and lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

Copy link

codecov bot commented Feb 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.87%. Comparing base (ac44d22) to head (ad21e6b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1306   +/-   ##
=======================================
  Coverage   96.87%   96.87%           
=======================================
  Files          23       23           
  Lines        1278     1282    +4     
  Branches      211      211           
=======================================
+ Hits         1238     1242    +4     
  Misses         21       21           
  Partials       19       19           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

ddabble
ddabble previously approved these changes Feb 17, 2024
Copy link
Member

@ddabble ddabble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks good!

I allowed myself to make some small improvements to the documentation you wrote; does it look okay to you?

Guess we could get this in with the 3.5.0 release..

docs/historical_model.rst Outdated Show resolved Hide resolved
docs/historical_model.rst Outdated Show resolved Hide resolved
@ddabble
Copy link
Member

ddabble commented Feb 19, 2024

@tim-schilling Just in case you didn't see my comments above :)

@tim-schilling
Copy link
Contributor Author

I definitely have. I should get to it this week. Thank you for the review!

@ddabble
Copy link
Member

ddabble commented Feb 19, 2024

Alright, we can release 3.5.0 in the meantime, then - unless you'd like this included? 🙂

@tim-schilling
Copy link
Contributor Author

Nope, no qualms on that.

Copy link
Member

@ddabble ddabble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 😊

@ddabble ddabble merged commit c39ef2a into jazzband:master Feb 24, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for custom history manager Extend source model's QuerySet manager to Historical model?
2 participants