I have two models: Item
and Photo
. Item
is foreignkey to Photo
so one item can have many photos connected which is quite usual.
Item class:
class Item(models.Model):
name = models.CharField('Item Name',
max_length = 150,
null = False,
blank = False
)
description = RichTextField('Item Description')
Photo class:
class Photo(models.Model):
path_and_rename = PathAndRename()
photo = models.ImageField(upload_to = path_and_rename, blank = True, null = True)
item = models.ForeignKey('Item', related_name = 'photo_set')
When I'm creating new Item
entry with bunch of photos I'd like to do some additional stuff: launching asynchronous task for indexing and email notification of user - some_async_task()
. I'm trying to do it by Item.save()
method:
def save(self):
super(Item, self).save()
some_async_task.delay(self.id)
@task(name="something")
def something(item_id):
print Item.objects.get(pk=item_id).photo_set.first().photo.url
It seems save()
method of Item model is exactly what I need but problem is that even after calling super()
I can't get data about related Photo entries because such relationship doesn't exist.
What is the best way to run method after Item entry was created and all related Photo entries were related?
post_save
signal doesn't fix situation.