#25559: Conditional admin inline causes ValidationError
--------------------------------+--------------------------------------
     Reporter:  zborboa-google  |                    Owner:  nobody
         Type:  Uncategorized   |                   Status:  closed
    Component:  contrib.admin   |                  Version:  1.8
     Severity:  Normal          |               Resolution:  wontfix
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------

Comment (by zborboa-google):

 Can we document overriding `change_view` is possible to achieve this?

 Here's a solution that works. Use `change_view` to conditionally display
 the inline. Replace `myapp/admin.py` above with this:

 {{{#!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 change_view(self, request, object_id, form_url='',
 extra_context=None):
         # Display inline when the object has been saved and a team has
 been selected.
         self.inlines = []
         try:
             obj = self.model.objects.get(pk=object_id)
         except self.model.DoesNotExist:
             pass
         else:
             if obj.team:
                 self.inlines = [CategoryInlineAdmin,]
         return super(StudentAdmin, self).change_view(request, object_id,
 form_url, extra_context)

 class TeamAdmin(admin.ModelAdmin):
     pass

 admin.site.register(Student, StudentAdmin)
 admin.site.register(Team, TeamAdmin)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25559#comment:3>
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/072.e5e6bb86a60bf72a11ea119aa7a91090%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to