我目前正在迁移到 Django 1.7。我有一些信号传递了一个未保存的模型实例,现在抛出TypeError: Model instances without primary key value are unhashable
.
我想知道 Djangopre_save
信号是如何在实例周围传递的?我正在查看文档,甚至找到了在 1.7 中实现的提交(https://github.com/django/django/commit/6af05e7a0f0e4604d6a67899acaa99d73ec0dfaa),我只是不知道它是如何工作的。
有人可以向我解释 pre_save 如何解决这个问题,或者我自己如何解决这个限制?谢谢。
下面的示例代码:
from django.dispatch import Signal
send_text = Signal()
unsaved_model = SomeModel() # note that neither `create` or `.save()` are being called
send_text.send(sender=unsaved_model) # error gets thrown when this gets called
追溯:
File "/home/ubuntu/fangsterr-app/notifications/models.py", line 43, in send
send_text.send(sender=self)
File "/home/ubuntu/virtualenvs/venv-2.7.5/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 194, in send
if not self.receivers or self.sender_receivers_cache.get(sender) is NO_RECEIVERS:
File "/home/ubuntu/virtualenvs/venv-2.7.5/lib/python2.7/site-packages/django/db/models/base.py", line 484, in __hash__
raise TypeError("Model instances without primary key value are unhashable")
TypeError: Model instances without primary key value are unhashable