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...@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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAH-SnCALGpZGrjVfvkN%2Bt-6Dg6tkX7yW-DhQxyMueTw9rHtqpg%40mail.gmail.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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cbdab94a9c2b481bbddb1b242009de80%40iss2.ISS.LOCAL.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/98d2c65cc3964e0181f4cc4d40a531ba%40iss2.ISS.LOCAL.