Django:保存后访问ManyToManyField对象
这让我感到困惑......当我保存模型时,书籍对象没有改变。但如果我打开发票并再次保存,更改就会发生。我做错了什么?
class Invoice(models.Model):
...
books = models.ManyToManyField(Book,blank=True,null=True)
...
def save(self, *args, **kwargs):
super(Invoice, self).save(*args, **kwargs)
for book in self.books.all():
book.quantity -= 1
if book.quantity == 0:
book.sold = True;
book.save()
编辑:我尝试过使用 post_save 信号,但其工作方式相同。第一次保存时没有更改,第二次保存更改。
更新:似乎可以用以下代码解决:
class InvoiceAdmin(admin.ModelAdmin):
...
def save_model(self, request, obj, form, change):
obj.save()
for bk in form.cleaned_data['books']:
book = Book.objects.get(pk=bk.id)
book.quantity -= 1
if book.quantity == 0:
book.sold = True;
book.save()
This is baffling me... When I save my model, the book objects are unchanged. But if I open the invoice and save it again, the changes are made. What am I doing wrong?
class Invoice(models.Model):
...
books = models.ManyToManyField(Book,blank=True,null=True)
...
def save(self, *args, **kwargs):
super(Invoice, self).save(*args, **kwargs)
for book in self.books.all():
book.quantity -= 1
if book.quantity == 0:
book.sold = True;
book.save()
Edit: I've tried using the post_save signal, but it works the same way. No changes on the first save, changes saved the second time.
Update: Seems to be solved with this code:
class InvoiceAdmin(admin.ModelAdmin):
...
def save_model(self, request, obj, form, change):
obj.save()
for bk in form.cleaned_data['books']:
book = Book.objects.get(pk=bk.id)
book.quantity -= 1
if book.quantity == 0:
book.sold = True;
book.save()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我解决这种令人困惑的行为的方法。将信号接收器连接到 models.signals.m2m_changed 事件,每次 m2m 字段更改时都会触发此事件。内嵌注释解释了原因。
This is how I worked around this, indeed baffling, behavior. Connect a signal receiver to models.signals.m2m_changed event, this get's triggered each time a m2m field is changed. The inline comments explain why.
这是因为m2m关系是在模型保存后保存的,以便获得父对象的PK。在您的情况下,第二次保存按预期工作,因为模型已经具有第一次保存的 PK 和关联书籍(它是在信号中完成的)。
我还没有找到解决方案,最好的办法是进行更改 在管理视图,我猜。
That's because m2m relation are saved after your model save, in order to obtain PK of parent object. In your case, second save works as expected because model already has PK and associated books from first save (it's done in a signal).
I haven't found the solution yet, best bet is to do your changes in admin view, i guess.