Tuesday, April 16, 2019

Re: Unhashable TypeError when deleted from model, Django 2.2

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

On Tue, Apr 16, 2019 at 9:29 PM Matthew Pava <Matthew.Pava@iss.com> wrote:

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'

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAH-SnCALGpZGrjVfvkN%2Bt-6Dg6tkX7yW-DhQxyMueTw9rHtqpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment