Edited :-) Hopefully a bit clearer now.
The logic is of the model is:
- A
Building
has manyRooms
- A
Room
may be inside anotherRoom
(a closet, for instance--ForeignKey on 'self') - A
Room
can only in inside of anotherRoom
in the same building (this is the tricky part)
Here's the code I have:
#spaces/models.py
from django.db import models
class Building(models.Model):
name=models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Room(models.Model):
number=models.CharField(max_length=8)
building=models.ForeignKey(Building)
inside_room=models.ForeignKey('self',blank=True,null=True)
def __unicode__(self):
return self.number
and:
#spaces/admin.py
from ex.spaces.models import Building, Room
from django.contrib import admin
class RoomAdmin(admin.ModelAdmin):
pass
class RoomInline(admin.TabularInline):
model = Room
extra = 2
class BuildingAdmin(admin.ModelAdmin):
inlines=[RoomInline]
admin.site.register(Building, BuildingAdmin)
admin.site.register(Room)
The inline will display only rooms in the current building (which is what I want). The problem, though, is that for the inside_room
drop down, it displays all of the rooms in the Rooms table (including those in other buildings).
In the inline of rooms
, I need to limit the inside_room
choices to only rooms
which are in the current building
being displayed by the main form.
I can't figure out a way to do it with either a limit_choices_to
in the model, nor can I figure out how exactly to override the admin's inline formset properly (I feel like I should be somehow create a custom inline form, pass the building_id of the main form to the custom inline, then limit the queryset for the field's choices based on that--but I just can't wrap my head around how to do it).
Maybe this is too complex for the admin site, but it seems like something that would be generally useful...
Thanks again for your help!