I'm working on my first Django app. My goal is to have two models, one for offices and one for employees, and when a person record is created (in the admin site), creating and specifying the room also populates that room with that person.
I was able to create the models, but now I'm stuck in a catch-22 where when I add a person, I have to create a room, but to create a room I have to add a person, etc...
How can I create and automatically populate a room like this?
Here is the relevant code from models.py
:
class Room(models.Model):
number = models.CharField('Room number', primary_key=True, max_length=50)
occupant = models.ManyToManyField('Person', blank=True) # Can have multiple occupants
class Person(models.Model):
name = models.CharField('Full name', max_length=200)
office = models.ForeignKey(Room, on_delete=models.SET_NULL, null=True) # Some offices may not have an occupant
Update: I added blank=True
to office
so I can create an office with no occupant. But, even when I add a person and assign their office
to that number, and it shows that in the admin page, the detail-view page doesn't seem to grab that, unless I manually go into the Room
admin page and select the person. Can that part be automated?