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

Foreign Key value in __str__ results in error. #533

Closed
dojutsu-user opened this issue Mar 3, 2019 · 3 comments
Closed

Foreign Key value in __str__ results in error. #533

dojutsu-user opened this issue Mar 3, 2019 · 3 comments

Comments

@dojutsu-user
Copy link

Describe the bug
I have two models like this:

class Book(models.Model):
    title = models.CharField(max_length=50)
    # other fields

class BookCode(models.Model):
    book_id = models.CharField(max_length=10)
    associated_book = models.ForeignKey(Book, on_delete=models.CASCADE)
    history = HistoricalRecords()
    # other fields

    def __str__(self):
        return f'[{self.book_id.upper}] {self.associated_book.title}'

When I try to get the history of model BookCode, this happens

>>> BookCode.history.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/pikachu/Desktop/Practice/library/venv/lib/python3.6/site-packages/django/db/models/query.py", line 247, in __repr__
    return '<%s %r>' % (self.__class__.__name__, data)
  File "/home/pikachu/Desktop/Practice/library/venv/lib/python3.6/site-packages/django/db/models/base.py", line 503, in __repr__
    return '<%s: %s>' % (self.__class__.__name__, self)
  File "/home/pikachu/Desktop/Practice/library/venv/lib/python3.6/site-packages/simple_history/models.py", line 386, in <lambda>
    self.history_object, self.history_date
  File "/home/pikachu/Desktop/Practice/library/portal/books/models.py", line 84, in __str__
    return f'[{self.book_id.upper()}] {self.associated_book.title}'
  File "/home/pikachu/Desktop/Practice/library/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 188, in __get__
    "%s has no %s." % (self.field.model.__name__, self.field.name)
books.models.BookCode.associated_book.RelatedObjectDoesNotExist: BookCode has no associated_book.

Expected behavior
Model history should be the output.

Environment:

  • OS: Ubuntu 18.04.2 LTS
  • Django Simple History Version: 2.7.0
  • Django Version: 2.1.7
  • Database Version: Using default sqlite3.
@rossmechanic
Copy link
Collaborator

Hi @dojutsu-user, the reason this fails is that the __str__ method for HistoricalBookCode is inherited from BookCode. HistoricalBookCode removed database constraints from its foreign keys, so HistoricalBookCode won't actually have an associated_book, so when you try to get the history for BookCode in your console, it fails. If you change the __str__ method so that it doesn't traverse any foreign keys, it'll fix the problem. Your other option would be to submit a PR to have a custom __str__ method for the history.

@alejandro-perez
Copy link

alejandro-perez commented Jan 30, 2023

I have a similar issue, that also results in a performance issue when listing a lot of changes.
If one has a __str__() method that uses a ForeignKey, it will never work if the related object has been removed, and also it will require a DB query for each element (apparently select_related() does not work here).

My suggestion is that a HistoricalRecord stores has a field called repr containing the textual representation of the object at the time the record was created.

If this sounds interesting I can try to create a PR for it

@dwasyl
Copy link

dwasyl commented May 4, 2023

Having a system full of models with __str__()s with FKs in them made me find this old outstanding issue. Is there any way to quickly add some sort of flag to ignore this, or just something that will be an issue without something like the PR above?

Especially for through models that might only have 2 ForeignKey fields, it adds some real challenges.

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

4 participants