Wednesday, May 29, 2013

Please ,How to pass request.user to wizard form field?

sorry ,please forgive me my poor english.

  I  customize a FriendMultiplySelectedField which inherited from forms.ModelMultipleChoiceField , to query a list friend for choice . 
when I added __init__  function in form  to accept request.usr  and then pass it to FriendMultiplySelectedField.  i meet a problem with form validation .
    I found  the reason maybe field wizardform prefix had lost when added __init__ function in Form.
    Did I do something wrong, I wonder how does wizardform process __ init__, and if  remove __init__ function in form ,how can i pass request.user to field. thanks

### my customize field
class FriendMultiplySelectField(forms.ModelMultipleChoiceField):
    def __init__(self, user=None, cache_choices=False, required=True,
                 widget=None, label=None, initial=None,
                 help_text=None, *args, **kwargs):
        if  user != None:
            queryset = Friend.objects.filter(to_user_id=user)
        else:
            queryset = Friend.objects

        super(FriendMultiplySelectField, self).__init__(queryset,
                                                        cache_choices, required,
                                                        widget, label, initial,
                                                        help_text, *args, **kwargs)


class ProjectSettingForm(forms.Form):
    publish_now =  forms.BooleanField(label="publish now",required=False,initial=True)
    publish_time = forms.DateTimeField(required=False,label="publish time")
    def __init__(self,*args,**kwargs):
        self.user = kwargs.pop("user")
        super(ProjectSettingForm, self).__init__( args,kwargs)
        self.fields["people"] = FriendMultiplySelectField(required=False,label="asign to",user=self.user)


class CreateTask(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT,'task_attachment'))
    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]
    def get_form_kwargs(self ,step=None):
        kwargs = super(CreateTask,self).get_form_kwargs(step)
        if step == self.steps.last:
            kwargs.update(user=self.request.user)
        return kwargs

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment