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

Accessing HistoricForeignKey relation after .as_instances() throws error #1272

Open
tahme opened this issue Oct 6, 2023 · 0 comments
Open

Comments

@tahme
Copy link

tahme commented Oct 6, 2023

Describe the bug

According to the documentation, these two should be equivalent

RankedDocument.history.as_of(t1)
RankedDocument.history.filter(history_date__lte=t1).latest_of_each().as_instances()

but if you use .as_instances() and try to access an object related through a HistoricForeignKey, an error is thrown:

ValueError("Cannot use None as a query value")

The stack trace points to manager.py, line 202, in as_of:

    queryset = self.get_queryset().filter(history_date__lte=date)

To Reproduce
Steps to reproduce the behavior:

  1. Create model with HistoricForeignKey, e.g.
class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published', default=timezone.now())
    history = HistoricalRecords()

class Choice(models.Model):
    poll = HistoricForeignKey(Poll, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    history = HistoricalRecords()
  1. Create a Poll and add a Choice
choice = Choice.objects.create(poll=Poll.objects.create())
  1. Get an instance from history using as_instances() and try to access .poll
choice.history.as_instances().first().poll # raises error
choice.history.first().instance.poll # works

I did notice that choice.history.first().instance has no attribute _history._as_of but choice.history.as_instances().first() has _history._as_of == None. I think this causes HistoryManager.as_of to be called with date=None.

Expected behavior
Accessing related object works after using .as_instances().

Environment (please complete the following information):

  • OS: Linux Mint 21.2
  • Django Simple History Version: 3.4.0
  • Django Version: 4.2.6
  • Database Version: sqlite3
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

No branches or pull requests

1 participant