﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25559	Conditional admin inline causes ValidationError	Zach Borboa	nobody	"Attempting to save an object in the Django admin using a conditional to show/hide an admin inline causes `ValidationError: ManagementForm data is missing or has been tampered with`.

1. Add a new student. Specify a name (""Alice"") and click the Save and continue editing button.
2. Now click the + to add a Team. Specify a team name (""Team A"") and click the Save button.
3. Back on the student page with the team now selected, click the Save and continue editing button.

Result: ValidationError

Expected: Student object to be saved and the inline to now be displayed.

{{{#!python
# myapp/models.py
from django.db import models

class Team(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=255)

class Student(models.Model):
    name = models.CharField(max_length=255)
    team = models.ForeignKey(Team, blank=True, null=True)
    categories = models.ManyToManyField(Category, through='CategoryInfo')

class CategoryInfo(models.Model):
    category = models.ForeignKey(Category)
    student = models.ForeignKey(Student)
    score = models.CharField(max_length=255)
}}}

{{{#!python
# myapp/admin.py
from django.contrib import admin

from models import Student
from models import Team

class CategoryInlineAdmin(admin.TabularInline):
    model = Student.categories.through

class StudentAdmin(admin.ModelAdmin):
    inlines = []
    def get_inline_instances(self, request, obj=None):
        inlines = self.inlines
        # Display inline when the object has been saved and a team has been selected.
        if obj and obj.team:
            inlines = [CategoryInlineAdmin,]
        return [inline(self.model, self.admin_site) for inline in inlines]

class TeamAdmin(admin.ModelAdmin):
    pass

admin.site.register(Student, StudentAdmin)
admin.site.register(Team, TeamAdmin)
}}}"	Uncategorized	closed	contrib.admin	1.8	Normal	wontfix			Unreviewed	0	0	0	0	0	0
