views:

26

answers:

1

I`ve been searching and trying to get this to work for the past few hours and I cant ...

I have a list of skills that I take from the database

forms.ModelMultipleChoiceField(
                              skills.objects.all(), 
                              required=True, 
                              widget=forms.CheckboxSelectMultiple(),
                              label='Check your skills.',
                              initial=skill_list.objects.filter(user='void'),
                              )

but this aint working as I expect it to work I basically wanna display the list of skills (all of them) and the ones that the user has checked to be checked ... pff I cant even explain right...

after the user checks the checkbox and hits submit in another page I wanna display all skills and the one the user checked to be checked ...

Lets say I have a b c and the user checks b and hits submit when in w/e page I wanna display a uncheked b checked c unchecked ...

Can some one help me with this ? Thx.

PS: if i use skills.objects.all() I get in the html value 1 and a how can I make this as

skills = (
         ('a', 'a'),
         ('b', 'b'),
         )  

instead of ('1', 'a'), ('2', 'b')

Thx a lot.

A: 

Your question(s) are a little unclear, but il still try my best.

Firstly,

I wanna display all skills and the one the user checked to be checked ...

The form doesnot have access to the user, so you need to pass it the user. You could do this by overloading the __init__ method like so:

    def __init__(self, **kwargs):
        current_user = kwargs.pop ('user')
        super(FormName, self).__init__(attrs)

then for setting the initially chosen values,

self.fields['skills'].initial = skill_list.objects.filter(user='void')

Remember you must do this after calling the constructor of the base class.

Finally,

PS: if i use skills.objects.all() I get in the html value 1 and a how can I make this as

skills = ( ('a', 'a'), ('b', 'b'), )

instead of ('1', 'a'), ('2', 'b')

Why would you want to do that? The numbers are the primary keys of the rows from you 'skills' table. Django uses this to setup the appropriate relationships.

zsquare
Why would I do that ? first of all thx for you reply ... after the user submits this form I wanna have the value a instead of 1 because I dont wanna query the db for what 1 is and based on that do smth ...Dont know if this is the right way, I could use the primary key but i`m lazy :D thx again for your reply :)
void