I have some interrelated models that need to co-exist on a single admin page. Here's the idea:
Theater productions have cast members, and cast members have specified roles. A theater production is related to a given written text (play, adaptation, etc.), and the written text holds a list of all the roles for that text. When adding a Production, each cast member needs to be associated with one of those roles.
Here's how the data model is working:
Models: Production, Person, CastMember, Role, WrittenText
Relationships: Production and Person have an M2M relationship through CastMember, which adds a "role" field - a ForeignKey to a Role object. Role itself has a ForeignKey to a WrittenText object.
So, the problem is this: in the admin page for Productions, I have a TabularInline to add CastMembers. The CastMember entries in the table should have their 'role' field limited to only the roles specified in the WrittenText that the Production references.
I made a half-way solution to the problem by overriding the model form:
class CastMemberForm(ModelForm): class Meta: model = CastMember def __init__(self, *args, **kwargs): super(CastMemberForm, self).__init__(*args, **kwargs) if 'instance' in kwargs: self.fields['role'].queryset = Role.objects.filter(source_text=self.instance.production.source_text)
But, this only works if you choose a Person from the drop-down, save, and then choose the role - otherwise you just get a list of all roles. Taking out "if 'instance' in kwargs" gives me a DoesNotExistError.
Is this just way too complex to do without something like client-side JS, or is there a simpler solution that I'm missing?Answer1: