Show models.ManyToManyField as inline, with the same form as models.ForeignKey inline

Posted by Kristian on Stack Overflow See other posts from Stack Overflow or by Kristian
Published on 2010-03-23T15:24:56Z Indexed on 2010/04/29 22:47 UTC
Read the original article Hit count: 528

I have a model similar to the following (simplified):

models.py

class Sample(models.Model):  
    name=models.CharField(max_length=200)  

class Action(models.Model):  
    samples=models.ManyToManyField(Sample)
    title=models.CharField(max_length=200)  
    description=models.TextField()  

Now, if Action.samples would have been a ForeignKey instead of a ManyToManyField, when I display Action as a TabularInline in Sample in the Django Admin, I would get a number of rows, each containing a nice form to edit or add another Action. However; when I display the above as an inline using the following:

class ActionInline(admin.TabularInline):
    model=Action.samples.through

I get a select box listing all available actions, and not a nifty form to create a new Action.

My question is really: How do I display the ManyToMany relation as an inline with a form to input information as described?

In principle it should be possible since, from the Sample's point of view, the situation is identical in both cases; Each Sample has a list of Actions regardless if the relation is a ForeignKey or a ManyToManyRelation. Also; Through the Sample admin page, I never want to choose from existing Actions, only create new or edit old ones.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models