title: Python Django: Filter within for loops
body: <p><strong>Context</strong></p>
  <p>I currently have 'Building' and 'Room' objects stored in my database - linked by a Foreign Key.</p>
  <p>I'm trying to generate a Room query set for each Building using for loops and objects.filter()
  <br />
  <br /></p>
  <p><strong>Problem</strong><br /></p>
  <p>See the code posted below - I can't seem to solve a seemingly simple problem.</p>
  <p>'BuildingList' returns two objects, but for some reason my for loop only queries and appends a 'RoomList' for one of two buildings. (The second one)</p>
  <p>I don't understand why, but <em>RoomList = Room_Data.objects.filter(Room_Building=b)</em> seems to disrupt the logic and flow of my basic for loop.</p>
  <br />
  <p><strong>Python Script:</strong></p>
  <pre><code>#
  #
  # 1.) GET DATA FOR RELEVANT SET OF BUILDINGS
  
  FusionBuildingName = &quot;Renal Dialysis Unit x6&quot;
  
  BuildingRoomLists = []
  
  
  
  # GET DATA FOR ALL BUILDINGS MATCHING 'FusionBuildingName'
  BuildingList = Building_Data.objects.filter(Building_Name=FusionBuildingName)
  
  print(len(BuildingList))
  print(BuildingList)
  
  
  # GENERATE BUILDING ROOM LISTS
  for b in BuildingList:
      
      print(b)
      print(&quot;Building Author: &quot; + b.Building_Author)
      
      print(&quot;Building Room List:&quot;)
      
      # GET LIST OF ROOM OBJECTS FOR CURRENT BUILDING
      RoomList = Room_Data.objects.filter(Room_Building=b)
      
      print(str(RoomList))
      
      # APPEND LIST OF ROOM OBJECTS TO BUILDING ROOM LIST
      BuildingRoomLists.append(RoomList)
      
  
  </code></pre>
  <p><strong>Below is what my Python Shell returns upon running the above code:</strong></p>
  <pre><code>In [43]:
  In [44]:     ...:     ...:     ...:     ...:     ...:     ...:     ...: Building_Data object (1)
  1
  Building Author : Angel Mahchut
  Building_Data object (2)
  2
  Building Author : AusHFG
  
  In [45]: Building Room List:
  
  In [46]:
  In [46]:
  In [46]:
  In [47]:
  In [48]:
  In [48]:
  In [48]: &lt;QuerySet [&lt;Room_Data: Room_Data object (41)&gt;, &lt;Room_Data: Room_Data object (42)&gt;, &lt;Room_Data: Room_Data object (43)&gt;, &lt;Room_Data: Room_Data object (44)&gt;, &lt;Room_Data: Room_Data object (45)&gt;, &lt;Room_Data: Room_Data object (46)&gt;, &lt;Room_Data: Room_Data object (47)&gt;, &lt;Room_Data: Room_Data object (48)&gt;, &lt;Room_Data: Room_Data object (49)&gt;, &lt;Room_Data: Room_Data object (50)&gt;, &lt;Room_Data: Room_Data object (51)&gt;, &lt;Room_Data: Room_Data object (52)&gt;, &lt;Room_Data: Room_Data object (53)&gt;, &lt;Room_Data: Room_Data object (54)&gt;, &lt;Room_Data: Room_Data object (55)&gt;, &lt;Room_Data: Room_Data object (56)&gt;, &lt;Room_Data: Room_Data object (57)&gt;]&gt;
  
  In [49]:
  In [49]:
  In [50]:
  </code></pre>
  <p>Despite looping through 2 building objects, 'Building Room List:' and '&lt;QuerySet [&lt;Room_Data:...' are only printed once - and for some reason the ordering of print outputs are incorrect?</p>
  <p>Any assistance is greatly appreciated. I've been stuck on this issue for a while now...</p>