访问Django中的父保存的ForeignKey对象 [英] Get access to ForeignKey objects at parent save in Django

查看:499
本文介绍了访问Django中的父保存的ForeignKey对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有添加到django中的模式的图像与内联编辑和外键相结合。我有这些模型(简化):

  class Modell(models.Model):
title = models.CharField 'beskrivelse',max_length = 200)
slug = models.SlugField()
is_public = models.BooleanField('publisert',default = True)

def __unicode __(self) :
return self.title

def save(self,** kwargs):
super(Modell,self).save(** kwargs)

on_modell_saved(self)

class Image(models.Model):
modell = models.ForeignKey(Modell,related_name =images)
image = ThumbnailImageField('bilde ',upload_to = get_upload_path_image)

class Meta:
verbose_name ='bilde'
verbose_name_plural ='bilder'

def __unicode __(self):
return str(self.image)

然后我用AdminInline将Image添加到Modell当我保存Modell我保存x的图像数量。



B当我尝试在on_modell_saved函数中使用Modell.images.all做一些事情时,我无法掌握对象。我有这个在Modell.save()执行的功能。$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ instance.images.all():
print img

这只会打印第二次我保存了Modell而不是第一次。所有任何人都知道如何调用一个功能,在您添加的所有项目之后,AdminInline被保存?

解决方案

Modell Image 是两个分开的模型,与 ForeignKey 连接。即使他们似乎都被保存在一起,但是他们没有。它们一个接一个地保存(首先是 Modell 或者第一个 Image s)。



现在逻辑上图像被保存 Modell 。因为他们有一个 ForeignKey 指向 Modell ,如果您尝试先保存它们,因为没有 Modell 是的,他们会指向什么(可以用 null = True )。



所以在时间 Modell.save()被称为 Image s不存在。 / p>

解决方案取决于你想做什么?我想这里的目的不仅仅是打印出 Image


I am trying to make a combined image of all images added to a modell in django with inline editing and a ForeignKey. Ive got these models (simplified):

class Modell(models.Model):
    title = models.CharField('beskrivelse', max_length=200)
    slug = models.SlugField()
    is_public = models.BooleanField('publisert', default=True)

    def __unicode__(self):
        return self.title

    def save(self, **kwargs):
        super(Modell, self).save(**kwargs)

        on_modell_saved(self)   

class Image(models.Model):
    modell = models.ForeignKey(Modell, related_name="images")
    image = ThumbnailImageField('bilde', upload_to=get_upload_path_image)

    class Meta:
        verbose_name = 'bilde'
        verbose_name_plural = 'bilder'

    def __unicode__(self):
        return str(self.image)

Then i add Image to the Modell with AdminInline, so when i save the Modell i save x number of images.

But when I try to do something with Modell.images.all in the on_modell_saved function i cant get hold of the objects. Ive got this function that is executed at Modell.save()

def on_modell_saved(instance):
    for img in instance.images.all():
        print img

This only prints something the second time i save the Modell and not the first time. So anybody know how to call a function after all items that you are adding with AdminInline is saved?

解决方案

Modell and Image are two seperate models connected with a ForeignKey. Even though they both seem to be saved together, they don't. They are saved one by one (first the Modell or first the Images).

Now logically Images are saved after Modell. Because they have a ForeignKey pointing to Modell and if you try to save them first, because there's no Modell yes, they would point to nothing (which is possible with null=True).

So by the time Modell.save() is called Images don't exist.

Solution depends on what you want to do? I suppose the aim here is not just printing out the Images.

这篇关于访问Django中的父保存的ForeignKey对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆