尝试保存模型实例时发生 KeyError。姜戈
尝试保存模型实例时发生 KeyError。 它必须对 post_save 信号做出反应,而不是保存实例...
代码:
from django.db.models.signals import post_save
class PlaylistEntry(models.Model):
playlist=models.ForeignKey(Playlist)
media=models.ForeignKey(Media)
order=models.PositiveIntegerField(default=9000000, editable=False)
added=models.DateTimeField(default=datetime.datetime.now(),editable=False )
def playlist_entry_changed(sender, instance, **kwargs):
entrys=PlaylistEntry.objects.filter(playlist=instance.playlist).order_by('order')
entrys[0].save()
post_save.connect(playlist_entry_changed, PlaylistEntry)
错误:
Exception Type: KeyError at /admin/playlist/playlistentry/add/
Exception Value: 38539456
KeyError when trying to save model instance.
It has to react on post_save signal than save instance...
Code:
from django.db.models.signals import post_save
class PlaylistEntry(models.Model):
playlist=models.ForeignKey(Playlist)
media=models.ForeignKey(Media)
order=models.PositiveIntegerField(default=9000000, editable=False)
added=models.DateTimeField(default=datetime.datetime.now(),editable=False )
def playlist_entry_changed(sender, instance, **kwargs):
entrys=PlaylistEntry.objects.filter(playlist=instance.playlist).order_by('order')
entrys[0].save()
post_save.connect(playlist_entry_changed, PlaylistEntry)
Error:
Exception Type: KeyError at /admin/playlist/playlistentry/add/
Exception Value: 38539456
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的评论,您想要做的是更新排序。不要使用信号,而是重写 save 方法。
另外,为什么你的订单默认是900万...不应该从0开始吗?
From your comments what you're trying to do is update ordering. Rather than use a signal, override the save method.
Also, why are default your order to 9 million... shouldn't you start with 0?
要实现排序,您还可以查看此代码段!
For implementig an ordering you can also have a look at this snippet!