#13883: SelectBox.js with grouping (optgroup elements)
-------------------------------------------+--------------------------------
          Reporter:  SardarNL              |         Owner:  nobody             
                      
            Status:  new                   |     Milestone:  1.3                
                      
         Component:  django.contrib.admin  |       Version:  1.2                
                      
        Resolution:                        |      Keywords:  admin, SelectBox, 
optgroup, sprintdec2010
             Stage:  Accepted              |     Has_patch:  1                  
                      
        Needs_docs:  0                     |   Needs_tests:  0                  
                      
Needs_better_patch:  0                     |  
-------------------------------------------+--------------------------------
Changes (by SardarNL):

  * needs_better_patch:  1 => 0

Comment:

 julien thanks for the test case.

 > except that the add popup (which appears after clicking the green cross
 icon) does not work properly.
 Fixed, but involves a little more changes than a thought first time.

 Drop-in replacement SelectBox.js has now fully compatible API as the
 original SlectBox.js, the group argument is optional. If there is no
 group, then option is added to 'not-grouped' option list.
 RelatedObjectLookups.js is updated to take optional group in account.

 A more painful update is needed for
 django.contrib.admin.options.ModelAdmin.response_add as it knows nothing
 about groups. Small patch looks for 'list_grouping' attribute, if it
 exists, then it is assumed to refer to the property returning the object's
 group. This assumes a group is a single property that can be turned into
 string.

 In short: if men needs just proper optgroup handling and doesn't mind if
 new objects will be added to 'no-group' list, then only SelectBox.js patch
 is needed.
 If men wants that new elements will be placed in the proper group, then
 RelatedObjectLookups.js and options.py patches are needed too. Of course
 this is only graphical issue.

 Updated test case:
 {{{
 from django.db import models

 class Sport(models.Model):
     name = models.CharField(max_length=100)
     is_team_sport = models.BooleanField(default=False)

     def __unicode__(self):
         return self.name

     @property
     def group(self):
         return 'Team Sports' if self.is_team_sport else 'Single Sports'

 class Profile(models.Model):
     sports = models.ManyToManyField(Sport)
 }}}

 Admin:
 {{{
 from django.contrib import admin
 from django.forms import ModelForm

 from .models import Sport, Profile

 class ProfileForm(ModelForm):
     def __init__(self, *args, **kwargs):
         super(ProfileForm, self).__init__(*args, **kwargs)

         choices = {}
         for sport in Sport.objects.all():
             choices.setdefault(sport.group, []).append(sport)
         self.fields['sports'].choices = choices.items()

 class ProfileAdmin(admin.ModelAdmin):
     form = ProfileForm
     filter_horizontal = ('sports',)

 class SportAdmin(admin.ModelAdmin):
     # currently is used only by response_add() but can be used by
 changelist_view() too
     list_grouping = 'group'


 admin.site.register(Profile, ProfileAdmin)
 admin.site.register(Sport, SportAdmin)
 }}}

 The ModelAdmin passes 'group' to RelatedObjectLookups.js, which passes it
 to SelectBox.js, which properly handles it.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13883#comment:5>
Django <http://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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to