Django Model.object.get pre_save 函数怪异
我制作了一个连接到模型“pre_save”信号的函数。 在函数内部,我尝试检查模型实例的 pk 是否已存在于表中:
sender.objects.get(pk=instance._get_pk_val())
模型的第一个实例引发错误。 我发现错误并从标题生成一个 slug 字段。 在第二种情况下,它不会抛出错误。 我检查了两个实例上的 instance._get_pk_val() 的值,它们是相同的:无
所以:
# This one raises an error in the sluggit function
instance1 = Model(title="title 1")
instance1.save()
# This one doesn't raise an error
instance2 = Model(title="title 2")
instance2.save()
这是我使用 python 和 django 的第三天。 所以,如果我没有看到一些新的东西,我很抱歉。
编辑:
模型:
class Test(models.Model):
title = models.CharField(max_length=128)
slug = models.SlugField(max_length=128)
slug.prepopulate_from=('title',)
signals.pre_save.connect(package.sluggit, sender=Test)
函数基础知识:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
sender.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
@S.Lot 告诉我重写注释中的 save() 方法。 我得尝试一下。 我仍然想知道为什么第二次调用 model.objects.get() 不会引发此方法的错误。
编辑2 谢谢@S.Lot。 重写保存方法效果很好。 对signal方法还是很好奇。 嗯,奇怪。
编辑3 多玩一会儿后,我发现使用 instance.objects.get() 而不是 sender.objects.get() 有效:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
sender.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
需要是:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
instance.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
一个错误? 出于某种原因,我认为 sender.objects.get() 与 Test.objects.get() 相同。
I have made a function that connects to a models 'pre_save' signal. Inside the function I am trying to check if the model instance's pk already exists in the table with:
sender.objects.get(pk=instance._get_pk_val())
The first instance of the model raises an error. I catch the error and generate a slug field from the title. In a second instance, it doesn't throw the error. I checked the value of instance._get_pk_val() on both instances and they are the same: None
So:
# This one raises an error in the sluggit function
instance1 = Model(title="title 1")
instance1.save()
# This one doesn't raise an error
instance2 = Model(title="title 2")
instance2.save()
This is my 3rd day messing around with python and django. So I am sorry if it something newbish that I am not seeing.
Edit:
The Model:
class Test(models.Model):
title = models.CharField(max_length=128)
slug = models.SlugField(max_length=128)
slug.prepopulate_from=('title',)
signals.pre_save.connect(package.sluggit, sender=Test)
The Function Basics:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
sender.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
@S.Lot told me to override the save() method in the comments. I'll have to try that. I would still like to know why the second call to model.objects.get() isn't raising an error with this method.
Edit 2
Thank you @S.Lot. Overriding the save method works perfectly. Still curious about the signal method. Hmm, weird.
Edit 3
After playing around a little more, I found that using instance.objects.get() instead of sender.objects.get() works:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
sender.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
needs to be:
def sluggit(sender, instance, signal, *args, **kwargs):
try:
instance.objects.get(pk=instance._get_pk_val())
except:
# Generate Slug Code
A bug? For some reason I thought sender.objects.get() would be the same as Test.objects.get().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
S.Lott 是正确的...使用
save()
,因为您已经承认您已经开始这样做。至于信号问题,老实说我看不出你的代码有什么问题。 我什至自己在本地运行并取得了成功。 您确定您在问题中正确地表达了它吗? 或者该 instance2 还不是现有的数据库对象(可能是您的测试代码中的一个错误)?
S.Lott is correct... use
save()
, as you've already acknowledged that you have started doing.As for the signal question, I can honestly see nothing wrong with your code. I've even run it locally myself with success. Are you sure that you're representing it properly in the question? Or that instance2 isn't already an existing database object (perhaps a goof in your test code)?
感谢您发布此内容。 顶级谷歌结果(在我发布此内容时)有点过时,并且显示了连接信号的旧方式(显然最近被重写了)。 您的编辑以及更正的代码片段向我展示了它是如何完成的。
我希望更多的发帖者编辑他们的评论以修复它。 谢谢。 :-)
Thanks for posting this. The top google results (at the time I'm posting this) are a little outdated and show the old way of connecting signals (which was recently rewritten, apparently). Your edits, with the corrected code snippets showed me how it's done.
I wish more posters edited their comments to place a fix in it. Thanks. :-)