tags:

views:

64

answers:

3

Hello, I've searched similar topics but haven't found what I need..

I extended Users model with UserAttributes model, some additional fields added and etc.. now I'm trying to make ModelForm out this.. so I have a little problem in here..

I WANT TO list groups as a ChoiceField not a MultipleChoiceField.. It's a requirement by specification so it must be so.

so here's the code..

from django.forms               import ModelForm
from django.contrib.auth.models import User
from helpdesk.models.userattributes      import *
from django.utils.translation import ugettext as _

class SettingsOperatorsForm(ModelForm):


    groups = forms.ChoiceField(
        label=_(u'Rights'),
        required=True,
        choices=["what's in here?"]
    )

    class Meta:
        model   = UserAttributes
        fields  = ('first_name', 'last_name', 'job_title', 'email', 'password', 'is_active', 'groups'  )

there's auth_group table in database, so i tried to make it like this , but I've got a no form displayed at all:

from django.contrib.auth.models import User, Group

groups = forms.ChoiceField(
    label=_(u'Rights'),
    required=True,
    choices=Group.objects.all()
)

I think it's better would be just to convert multipleChoiceField to ChoiceField

in plain talk:

alt text

should become just SELECT box.

A: 

Just a feedback, because 2hours no answers, so thnx to freenode #django =)

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

It's possible to override widgets for any field in Meta Class like this:

from django.forms import ModelForm

class AuthorForm(ModelForm):
    class Meta:
        model = Author
        widgets = { 'groups': forms.Select(),}

DOESN'T WORK

forms.ChoiceField() too.. nothing changed.. still displaying multichoice select..

UPDATE

from django.contrib.auth.models import User, Group
groups = forms.ChoiceField(
        required=True,
        choices = [ [g.id, g.name] for g in Group.objects.filter() ]
    ) 

This works.. it's ok but, why the hell widget override doesn't work????

RESOLVED because I have django 1.1 =( my stupidity..

holms
+2  A: 

Setting the choices at form definition time, as you do in your answer, will mean that the form will never see any new Groups that are defined.

Rather than using a ChoiceField with a list comprehension for choices, you should use a ModelChoiceField with a queryset parameter:

groups = forms.ModelChoiceField(queryset=Group.objects.all())
Daniel Roseman
I had this problem yesterday, still couldn't figure out. =) thnx for reply.
holms
A: 

it appears that it's that modelchoice would be wrong because current group in edit form will not be selected="Selected"

so this is solution FINALLY:

groups = forms.ModelMultipleChoiceField( 
    queryset=None, 
    required=True, 
    widget=GroupsSelect,
)

 def __init__(self, *args, **kw):  
    super(ModelForm, self).__init__(*args, **kw)
    self.fields['groups'].queryset=Group.objects.filter(user=self.instance.id)

#view
op = UserAttributes.objects.get(id=operator_id)
form = SettingsOperatorsForm(instance=op)
holms