Django:保存后访问ManyToManyField对象

时间:2010-02-04 06:23:00

标签: django many-to-many save m2m

令我困惑的是......当我保存模型时,书籍对象不变。但是,如果我打开发票并再次保存,则会进行更改。我做错了什么?

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()

2 个答案:

答案 0 :(得分:4)

这就是我解决这个问题的方式,确实令人费解。将信号接收器连接到models.signals.m2m_changed事件,每次更改m2m字段时触发此get。内联注释解释了原因。

class Gig(models.Model):
    def slugify(self):
        # Add venue name, date and artists to slug
        self.slug = slugify(self.venue.name) + "-"
        self.slug += self.date.strftime("%d-%m-%Y") + "-"
        self.slug += "-".join([slugify(artist.name) for artist in self.artists.all()]) 
        self.save()


@receiver(models.signals.m2m_changed, sender=Gig.artist.through)
def gig_artists_changed(sender, instance, **kwargs):
    # This callback function get's called twice. 
    # 1 first change appears to be adding an empty list
    # 2nd change is adding the actual artists
    if instance.artist.all() and not instance.slug:                                                                                                                                                               
        instance.slugify()

答案 1 :(得分:1)

这是因为在模型保存后保存m2m关系,以获得父对象的PK。在您的情况下,第二次保存按预期工作,因为模型已经具有PK和第一次保存的相关书籍(它在信号中完成)。

我还没有找到解决方案,最好的办法是进行更改in admin view,我猜。