73988

Django admin inline forms - limit foreign key queryset to a set of values

Question:

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:

<a href="http://djangosnippets.org/snippets/1028/" rel="nofollow">Here</a> is an example of chained select boxes via javascript/ajax. It should basically be the same principle, but you should need to tweak the js to not update one select box, but all of them in the inline admin... Maybe this gives you a small inspiration!

Recommend

  • How to .update m2m field in django
  • How to access Django ForeignKey model field?
  • json issue in HIVE
  • Is there a way to emulate the Java behaviour of calling a parent class' static method for simpl
  • MVC Cascading Dropdown using JQuery AJAX
  • New rails installation with error when entering url
  • How can I modify the shader in the iPhone OpenGL ES template to produce this effect?
  • Flask multiple optional route parameter [closed]
  • Mongoose document update error
  • How to partition a MyISAM table by day in MySQL
  • flask optional url parameter not working [closed]
  • How to multiply big numbers faster?
  • How to Format XAxis values correctly in Line Chart in aChartEngine in android
  • Prolog: Decrementing variable in argument
  • Visual Basic Vending Machine
  • mod-rewrite rule for sitemap.xml
  • Can I somehow use the to_field parameter in a Django ManyToMany field?
  • Is need install mysql on AWS EC2 if i am using AWS rds as database instance?
  • How to determine the association between a VB6 app and an exe instanced with CreateObject()
  • How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API
  • Detaching entity along with referenced entities
  • Change navbar in bootstrap if user login
  • Yii2 Login with database
  • Mockery and Laravel constructor injection
  • CoreData basics – to-many relationship array data
  • How to access recipient on sent messages page with mailboxer
  • Action Pack components in Rails
  • SetWindowsHookEx does not react on media keys
  • Looking for good analogy/examples for monitor verses semaphore
  • Button click event not firing in jQuery
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • output of program is not same as passed argument
  • Weird JavaScript statement, what does it mean?
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • EntityFramework adding new object to nested object collection
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app