1

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?

tutuca
  • 3,444
  • 6
  • 32
  • 54
espenhogbakk
  • 11,508
  • 9
  • 39
  • 38

3 Answers3

3

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.

muhuk
  • 15,777
  • 9
  • 59
  • 98
  • Yeah, that sounds about right, of course there need to be a Modell before the Image can be saved and related to the modell. No the aim is not just to print out the images. In Images ive overrided the save to make thumbs, and in the Modell save, i want to combine all these images to a filmstrip. – espenhogbakk Dec 06 '08 at 23:26
  • I don't why it has to be Modell.save() as long as the filmstrip is updated. I wish you had stated this in your question. Because in this case all you have to do is to call a Modell.update_filmstrip() function from ImageManager.create() and Image.delete() (assuming images are immutable) – muhuk Dec 07 '08 at 16:03
1

The Modell instance isn't being saved when you add a new image; only the Image instance is being saved.

Move your function call into the Image model and you'll be good.

def on_image_saved(instance):
    for img in Image.objects.filter(modell=instance.modell)
        print img

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)

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

        on_image_saved(self)
Daniel Naab
  • 22,690
  • 8
  • 54
  • 55
  • I think you misunderstood, because i newer add images manually, but i add images at the same time as i add a new Modell. I add images inline in the add Modell form. So then the Modell is saved, isnt it? If you do it your way, the on_image_saved is called one time for each image, i dont want that... – espenhogbakk Dec 06 '08 at 21:43
  • The foreign key is on the Image table, not the Modell table, so the image can't be added until the Modell it links to is created. They will be added one at a time, after the Modell instance I don't see this as a problem. But if it is, there are other hooks provided by the admin app that may help. – Daniel Naab Dec 06 '08 at 22:23
0

I dit solve it somehow, but it is farm from optimal, I did the modell_on_save function on Image save, this works, but the bad thing that if there are 5 images related and saved to the modell, there will be 5 calls to the function, and that is bad. I would like it to only happen after the last one...

espenhogbakk
  • 11,508
  • 9
  • 39
  • 38
  • That's the way to do it. The save() method has nothing to do with the admin interface.. it's called on all model instance saves, irregardless of it's source. If you hooked just on the admin save, that would have the problem of not working if you add an image from the shell or by another method. – Daniel Naab Dec 07 '08 at 13:52