Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:35:55+00:00 2026-05-26T20:35:55+00:00

I have multiple models which relate back to a single model. On save of

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:35:56+00:00Added an answer on May 26, 2026 at 8:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently managing multiple models from a single SITE MANAGER page. I have the
I have four models in my models.py which are: models.py class Course(models.Model): course_code =
I've built a simple Friend model, which allows Users to have multiple friends. Here's
I have a method that I've started to use in multiple models for Webscraping,
We have multiple MFC apps, which use CMutex( false, blah ), where blah allows
I need to have multiple submit buttons. I have a form which creates an
I have a problem with needing to provide multiple model backed forms on the
I want to represent clubs which have multiple members (each member can also join
Given a table of models 'A', that can have multiple child models 'B', of
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.