#25559: Conditional admin inline causes ValidationError
--------------------------------+--------------------
     Reporter:  zborboa-google  |      Owner:  nobody
         Type:  Uncategorized   |     Status:  new
    Component:  Uncategorized   |    Version:  1.8
     Severity:  Normal          |   Keywords:
 Triage Stage:  Unreviewed      |  Has patch:  0
Easy pickings:  0               |      UI/UX:  0
--------------------------------+--------------------
 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)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25559>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.97458e83848df1a144f720094141b4d2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to