question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model instances without primary key value are unhashable in admin panel

See original GitHub issue

I have implemented a “backup” functionality when a user wants to keep a revision of his object along with all the related ones. I have implemented the admin integration and registered all the “follows” correctly. When the user presses the “create new backup” button, then the following code is executed:

    with reversion.create_revision():
        main_object.save()

        reversion.set_user(request.user)
        reversion.set_comment(comment)

and a revision is created for the object and all the related ones.

The problem appears when I create a new object that has a relationship to the main_object. When the new object is created, if the user presses “Create new Backup”, then a revision is successfully created for the main_object and all the related ones BUT the previous revisions show this message on the admin panel: "Model instances without primary key value are unhashable".

The traceback that appears is:

File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/reversion/admin.py" in revision_view
  238.             context,
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/reversion/admin.py" in _reversion_revisionform_view
  184.                 version.revision.revert(delete=True)
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/reversion/models.py" in revert
  89.                         if item not in old_revision:
File "/Users/platico/PycharmProjects/news-service/src/lib/python2.7/site-packages/django/db/models/base.py" in __hash__
  521.             raise TypeError("Model instances without primary key value are unhashable")

with an Exception Type of TypeError and an Exception Value of Model instances without primary key value are unhashable

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
etianencommented, Nov 20, 2017

Cool, I’ll aim for a release sometime next week! Thanks for your help!

On 20 November 2017 at 09:13, Roman Karpovich notifications@github.com wrote:

@etianen https://github.com/etianen seems that fix working. thanks alot.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/etianen/django-reversion/issues/678#issuecomment-345634893, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJFCLmdzNCgsJHFCEthXXt8YJtQixcXks5s4ULSgaJpZM4Qex_Y .

1reaction
roman-karpovichcommented, Nov 17, 2017

Same problem for me. Multi-table inheritance.

import reversion
from django.db import models


@reversion.register(follow=('diaryevent_set', ))
class Diary(models.Model):
    pass


@reversion.register(follow=('specialdiaryevent', ))
class DiaryEvent(models.Model):
    diary = models.ForeignKey(Diary)


@reversion.register(follow=('diaryevent_ptr', ))
class SpecialDiaryEvent(DiaryEvent):
    pass
diary = Diary.objects.create()

with reversion.create_revision():
    reversion.add_to_revision(diary)

SpecialDiaryEvent.objects.create(diary=diary)

with reversion.create_revision():
    reversion.add_to_revision(diary)

reversion.models.Version.objects.get_for_object(diary)[1].revision.revert(delete=True)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Model instances without primary key value are unhashable ...
I am getting the error Model instances without primary key value are unhashable when trying to remove a model instance from my admin...
Read more >
23857 ("Save as new" breaks when related inlines are marked ...
... key value are unhashable") Exception Type: TypeError at /admin/myapp/book/1/ Exception Value: Model instances without primary key value are unhashable.
Read more >
Model instances without primary key value are unhashable in ...
I have implemented a "backup" functionality when a user wants to keep a revision of his object along with all the related ones....
Read more >
Model Instances Without Primary Key Value Are Unhashable
Model instances without primary key value are unhashable in admin panel # of TypeError and an Exception Value of Model instances without. Model...
Read more >
django 1.7 - help with change in "Save as new" behavior ...
class EventDateInline( admin. ... Model instances without primary key value are unhashable ... Are you using a custom form or custom template at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found