wilbbe01
wilbbe01

Reputation: 1971

Post save, model id nonexistent

I have multiple models which relate back to a single model. On save of these models I have overridden save to retrieve the id of the main model so as to place files on the OS in a directory keyed by the main model's pk.

For example, take a Building with many rooms. Any images of rooms will be saved in the directory keyed by the building's id (no subdirectory for rooms).

My overridden save methods work fine as long as the Building exists when the room is saved. If however the Building is not yet saved, and I am adding a room to the building via the django admin, the image remains in an upload directory, as no pk for the building exists yet.

I initially tried overriding save on Building and moving any Room images to a newly created Building directory (again keyed off building's pk). Despite super(Building, self).save(*args, **kwargs) first the Building's id was not set.

I then decided post_save signal was probably cleaner anyway and did such. Unfortunately, the id doesn't seem to exist in the post save either. I can attempt to print the ID and see no value when post save is triggered until I have saved the model for a second time.

Could someone point me in a direction that might explain why id is not getting set as would be expected output as accepted in this other SO answer?

Thanks.

Edit:

Here is some code as requested in a comment. I'm including a bit more here as I had simplified the initial question. Here there are 3 levels, a listing with buildings with rooms. The listing is what I attempt to simply print via the print kwargs['instance'] line. At the bottom I have included the output after two back to back saves. Notice the complete lack of an instance in existence after the first save. These were literally back to back with no actions between. References to things like Building_Room are through tables. RoomImage, BuildingImage, and ListingImage are all alike aside from data fields and I have therefore only included one.

class Listing(models.Model):
    ...
    buildings = models.ManyToManyField('Building', null=True, blank=True, through = 'Building_Listing')
    addresses = models.ManyToManyField(Address, null=True, blank=True)
    def __unicode__(self):
        return '  &  '.join([a.__unicode__() for a in self.addresses.all()])

class Building(models.Model):
    ...
    rooms = models.ManyToManyField('Room', null=True, through="Building_Room")
    def __unicode__(self):
        return self.description

class Room(models.Model):
    ...
    def __unicode__(self):
        return str(self.room_type)

class RoomImage(models.Model):
    room = models.ForeignKey(Room)
    room_photo = FileBrowseField("Image", max_length=200, blank=True, null=True)

    def save(self, *args, **kwargs):
        try:
            listing = Building_Listing.objects.get(building=Building_Room.objects.get(room=self.room).building).listing
            self.room_photo = moveFileBeforeSave(listing, self.room_photo)
        except Building_Listing.DoesNotExist:
            pass
        except Building_Room.DoesNotExist:
            pass
        super(RoomImage, self).save(*args, **kwargs)

@receiver(post_save, sender=Listing, weak=False)
def save_images_on_listing_create(sender, **kwargs):
    #if kwargs['created']:
    listing = kwargs['instance']
    print kwargs['instance']
    listing_image_list = ListingImage.objects.filter(listing = listing)
    listing_buildings = Building_Listing.objects.filter(listing = listing).values_list('building', flat=True)
    building_image_list = BuildingImage.objects.filter(building__in = listing_buildings)
    building_rooms = Building_Room.objects.filter(building__in = listing_buildings).values_list('room', flat=True)
    room_image_list = RoomImage.objects.filter(room__in = building_rooms)
    for image in listing_image_list:
        image.save()
    for image in building_image_list:
        image.save()
    for image in room_image_list:
        image.save()

@receiver(post_save, sender=Building, weak=False)
def save_images_in_building_create(sender, **kwargs):
    #if kwargs['created']:
    print str(kwargs['instance'])+" : building save trigger"
    building = kwargs['instance']
    building_image_list = BuildingImage.objects.filter(building = building)
    building_rooms = Building_Room.objects.filter(building = building).values_list('room', flat=True)
    room_image_list = RoomImage.objects.filter(room__in = building_rooms)
    for image in building_image_list:
        image.save()
    for image in room_image_list:
        image.save()

Some output:

[30/Oct/2011 19:52:05] "POST /admin/mls/building/add/?_popup=1 HTTP/1.1" 200 97
# This is the print of the instance kwarg after the first save (ie nothing)
[30/Oct/2011 19:52:10] "POST /admin/mls/listing/add/ HTTP/1.1" 302 0
[30/Oct/2011 19:52:10] "GET /admin/mls/listing/8/ HTTP/1.1" 200 72495
[30/Oct/2011 19:52:10] "GET /admin/jsi18n/ HTTP/1.1" 200 2158
1 Ben Blvd sometown, MN #this is the print of the instance kwarg after the second save
[30/Oct/2011 19:52:12] "POST /admin/mls/listing/8/ HTTP/1.1" 302 0
[30/Oct/2011 19:52:13] "GET /admin/mls/listing/8/ HTTP/1.1" 200 72497
[30/Oct/2011 19:52:13] "GET /admin/jsi18n/ HTTP/1.1" 200 2158

Upvotes: 2

Views: 2047

Answers (2)

wilbbe01
wilbbe01

Reputation: 1971

First, pastylegs is right and his answer is better. But if for some reason (like me right now) are unable to change code due to whatever reason and find yourself in the post_save model instance nonexistent problems like we have found ourselves in the past few days the following idea may be of assistance.

In our case the manytomany through table was sufficient enough for the post_save. By attaching the post save signal to the through table itself we were basically able to catch all cases we needed to do post_saving for, and being as the ids to the two connected tables for the manytomany relationship existed in the through table this was sufficient to get the job done. So if you find yourself here for similar reasons, can you attach the post_save to a through table instead?

Again, pastylegs is right, but if you cannot do the same for whatever reason I hope this is helpful.

Upvotes: 0

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53999

Ok, so the problem looks like it is because of the manytomany relationship you are using. Have a look at these post:

save with many-to-many relationship in django problem

Issue with ManyToMany Relationships not updating inmediatly after save

I'd consider refactoring you code to change the relationship between a building and it's rooms. At the moment you are saying 'there are many rooms and a building can associate itself with a number of those rooms'. Furthermore, two buildings can be associated with the same room. This doesn't really make sense. Really a room should be associated with one and only one buildling, i.e.

class Building(models.Model):
    name = models.CharField(...)
    ...

class Room(models.Model):
    building = models.ForeignKey(Building, unique=True)
    ....

this means that any room can only be linked to one particular building.

Upvotes: 2

Related Questions