Unhashable TypeError when deleted from model, Django 2.2

666 views
Skip to first unread message

Matthew Pava

unread,
Apr 16, 2019, 3:29:49 PM4/16/19
to django...@googlegroups.com

I have a model that has a nullable field called expires. If expires is null, then the record never expires.

I’m performing a delete on the model for any record that has an expires date that has passed, and I keep getting a TypeError that Model is unhashable. Am I doing something wrong?

 

Model.objects.filter(expires__lt=timezone.now()).delete()

 

Here’s the traceback:

 

Traceback (most recent call last):

  File "\django\core\handlers\exception.py", line 34, in inner

    response = get_response(request)

  File "\django\core\handlers\base.py", line 115, in _get_response

    response = self.process_exception_by_middleware(e, request)

  File "\django\core\handlers\base.py", line 113, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

  File "\django\views\decorators\csrf.py", line 54, in wrapped_view

    return view_func(*args, **kwargs)

  File "\django\views\generic\base.py", line 71, in view

    return self.dispatch(request, *args, **kwargs)

  File "\rest_framework\views.py", line 495, in dispatch

    response = self.handle_exception(exc)

  File "\rest_framework\views.py", line 455, in handle_exception

    self.raise_uncaught_exception(exc)

  File "\rest_framework\views.py", line 492, in dispatch

    response = handler(request, *args, **kwargs)

  File "\rest_framework\decorators.py", line 55, in handler

    return func(*args, **kwargs)

  File "C:\project\general\views.py", line 88, in all_messages_for_user

    Model.objects.filter(expires__lt=timezone.now()).delete()

  File "\django\db\models\query.py", line 710, in delete

    collector.collect(del_query)

  File "\django\db\models\deletion.py", line 192, in collect

    reverse_dependency=reverse_dependency)

  File "\django\db\models\deletion.py", line 94, in add

    if obj not in instances:

TypeError: unhashable type: 'Model'

Sithembewena L. Dube

unread,
Apr 16, 2019, 3:50:19 PM4/16/19
to django...@googlegroups.com
Why not use a boolean field on the model to denote expires? Then if that field is True, delete the object? I think that would be cheaper and cleaner, especially with lookups.

Kind regards,
Sithu


Sent with Shift

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f2d2e610ffe04dff85edf80be0c7ad2d%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
Apr 17, 2019, 12:19:47 PM4/17/19
to django...@googlegroups.com

Hi Sithu,

I would still need to a way determine when a record expires. A Boolean wouldn’t help in that regard.

But I think this error has something to do with Django’s delete method. The filtering aspect works just fine.

Matthew Pava

unread,
Apr 17, 2019, 3:49:12 PM4/17/19
to django...@googlegroups.com

Okay, the model I’m using specifies an __eq__ function without a corresponding __hash__ function. I’ve opened a pull request with the owner of that project.

Sithembewena L. Dube

unread,
Apr 17, 2019, 3:54:29 PM4/17/19
to django...@googlegroups.com
Interesting. Good catch.


Kind regards,
Sithu


Sent with Shift
Reply all
Reply to author
Forward
0 new messages