On Jul 3, 8:13 am, urukay <[EMAIL PROTECTED]> wrote:
> no, no i mean it otherway, just like Rajesh Dhawan wrote (link he sent).

I will rephrase your original question; it's not obvious to everyone
that the REASON you want two of your six choices to be "not
selectable" is because they are CATEGORIES or HEADERS intended to
separate the choices, NOT actual choices themselves.

But you DO still want them to appear in the dropdown as visual
separators, which Rajesh's link (http://www.w3schools.com/tags/
tag_optgroup.asp) illustrates nicely.

To that end, it would be better to reorganize your choices
hierarchically:

CHOICES = [
     ('0', "Black"),
     ('255', "White"),
     ('Basic Colors', [
                       ('1', "Red"),
                       ('2', "Green"),
                       ('3', "Blue") ] ),
     ('Other Colors', [
                       ('5', "Brown"),
                        ..... ] )
]

Now your question becomes,
"Is the select-dropdown widget in Django sophisticated enough to
realize that if an element in the choices list contains a list or
tuple (rather than a string) value, it should render that as an
<optgroup> and recurse into the NESTED list?"

The answer is "no", but it doesn't choke on it either.
This would be an excellent candidate for a custom widget & renderer, I
think.

http://www.djangoproject.com/documentation/newforms/#custom-widgets

You could subclass the renderer methods of the default selection-
dropdown renderer and add "smarts" to make it generate <optgroup> tags
and recurse appropriately, when it detected a nested list or tuple.

Not exactly trivial, but it's not as difficult as you might think,
once you start snooping around in the Django source code.

I'm a novice myself and have only written a renderer subclass to put
radio-button labels to the left of the button instead of to the right,
so I won't be much help if you run into trouble...
but hopefully my suggestion will open up an avenue of exploration that
might not have occurred to you.  Here's a snippet to show how easy it
was:


from django.newforms.widgets import RadioInput, RadioFieldRenderer

class PrelabelRadioInput(RadioInput):
    def __unicode__(self):
      ...

class PrelabelRadioFieldRenderer(RadioFieldRenderer):
    def __iter__(self):
        for i, choice in enumerate(self.choices):
            yield PrelabelRadioInput(self.name, self.value,
self.attrs.copy(), choice, i)

    def __getitem__(self, idx):
        choice = self.choices[idx]
        return PrelabelRadioInput(self.name, self.value,
self.attrs.copy(), choice, idx)

class MyForm(forms.Form):
    mything =
forms.ChoiceField(widget=RadioSelect(renderer=PrelabelRadioFieldRenderer), ...)
    ...

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to