choices

10 views
Skip to first unread message

urukay

unread,
Jul 2, 2008, 4:40:02 PM7/2/08
to django...@googlegroups.com

is there a way how to create choices in Models in specific way, that some
choices are not selectable (they only group and describe choices below)?

e.g.:

CHOICES = (
('0', 'Basic Colors'),
('1', 'Red'),
('2', 'Green'),
('3', 'Blue'),
('4', 'Other Colors'),
('5', 'Brown'),
.....
)

and I want "Basic Colors" and "Other Colors" not selectable when displaying
CHOICES in model. Is it possible?

Thanks


Radovan
--
View this message in context: http://www.nabble.com/choices-tp18246312p18246312.html
Sent from the django-users mailing list archive at Nabble.com.

Rajesh Dhawan

unread,
Jul 2, 2008, 6:42:14 PM7/2/08
to Django users


On Jul 2, 4:40 pm, urukay <radovan.be...@gmail.com> wrote:
> is there a way how to create choices in Models in specific way, that some
> choices are not selectable (they only group and describe choices below)?
>
> e.g.:
>
> CHOICES = (
>      ('0', 'Basic Colors'),
>      ('1', 'Red'),
>      ('2', 'Green'),
>      ('3', 'Blue'),
>      ('4', 'Other Colors'),
>      ('5', 'Brown'),
>         .....
> )
>
> and I want "Basic Colors" and "Other Colors" not selectable when displaying
> CHOICES in model. Is it possible?

If you want to do this in your own template, you could use the
OPTGROUP HTML tag when rendering that formfield:

http://www.w3schools.com/tags/tag_optgroup.asp

urukay

unread,
Jul 2, 2008, 8:27:37 PM7/2/08
to django...@googlegroups.com

thanks, but i was wondering if it's possible to do that in MODEL?

--
View this message in context: http://www.nabble.com/choices-tp18246312p18249595.html

Malcolm Tredinnick

unread,
Jul 2, 2008, 9:03:43 PM7/2/08
to django...@googlegroups.com

On Wed, 2008-07-02 at 13:40 -0700, urukay wrote:
>
> is there a way how to create choices in Models in specific way, that some
> choices are not selectable (they only group and describe choices below)?
>
> e.g.:
>
> CHOICES = (
> ('0', 'Basic Colors'),
> ('1', 'Red'),
> ('2', 'Green'),
> ('3', 'Blue'),
> ('4', 'Other Colors'),
> ('5', 'Brown'),
> .....
> )
>
> and I want "Basic Colors" and "Other Colors" not selectable when displaying
> CHOICES in model. Is it possible?

Just pass in the choices you want to the "choices" attribute of the
model field. Nothing requires you to have all your choices only in a
single list. And, if later, you wish to combined your various sublists
into one list for some reason, then just concatenate the individual
lists.

Don't over-think this: you want to pass in a particular small list to a
model field, so just create that list and pass it in. No complex
technology required.

Malcolm


urukay

unread,
Jul 3, 2008, 4:24:46 AM7/3/08
to django...@googlegroups.com

yes, but it's only for limpidity :) and I need the user to chose only one
option from the list. Just can't count on that user wil chose to fill only
one field with choices if there are more possible fields with choices.
And it isn't possible to add to oe field multiple choices lists (i mena
somethink like choices1 =..., choices2 = ... etc), is it?
Or maybe completely misapprehended what u wrote me :D but really, thanks a
lot anyway :)

--
View this message in context: http://www.nabble.com/choices-tp18246312p18254242.html

Alessandro Ronchi

unread,
Jul 3, 2008, 8:21:54 AM7/3/08
to django...@googlegroups.com
2008/7/3, urukay <radova...@gmail.com>:

>
>
> yes, but it's only for limpidity :) and I need the user to chose only one
> option from the list. Just can't count on that user wil chose to fill only
> one field with choices if there are more possible fields with choices.
> And it isn't possible to add to oe field multiple choices lists (i mena
> somethink like choices1 =..., choices2 = ... etc), is it?
> Or maybe completely misapprehended what u wrote me :D but really, thanks a
> lot anyway :)

You can do:

CHOICES1 = (


('1', 'Red'),
('2', 'Green'),
('3', 'Blue'),

('5', 'Brown'),
)

CHOICES2 = (


('0', 'Basic Colors'),

('4', 'Other Colors'),

)

and pass what you want to the model.

If you want the user to select only C1 you can do choices = CHOICES1

and if you want to select all the set you can do choices = CHOICES1 + CHOICES2

--
Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net

SOASI Soc.Coop. - www.soasi.com
Sviluppo Software e Sistemi Open Source
Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

urukay

unread,
Jul 3, 2008, 9:13:32 AM7/3/08
to django...@googlegroups.com

no, no i mean it otherway, just like Rajesh Dhawan wrote (link he sent).

guess i'll have to find other way to that, not in Model level.

but thanks

--
View this message in context: http://www.nabble.com/choices-tp18246312p18259026.html

rskm1

unread,
Jul 3, 2008, 4:41:05 PM7/3/08
to Django users
On Jul 3, 8:13 am, urukay <radovan.be...@gmail.com> 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), ...)
...
Reply all
Reply to author
Forward
0 new messages