On Dec 8, 8:00Â am, chewynougat <[EMAIL PROTECTED]> wrote: > Could anyone tell me if I can pass the current user to a form wizard > step so I can prepopulate their object details in the form? If so, > how?
It depends on where exactly you need to access the current user. If you can do it *after* form validation has been completed and just use ``commit=False`` [1] when saving the form to fill in the extra data, that's easily accomplished in the ``done()`` method of the form [2]. However, if you need the current user right when the form is instantiated (if you need a pre-populated form to edit an existing object, for example) you're in for a bit of extra work. Here's a FormWizard class that I use to pass the ``instance`` [1] keyword to all the ModelForms in a Wizard. (Obviously you'll need to do a bit of extra leg-work if all of your forms don't derive from the same model, or aren't ModelForms at all.):: class YourFormWizard(FormWizard): def __call__(self, request, *args, **kwargs): """Override for getting instance to get_form.""" self.instance = kwargs.pop('instance', None) return super(YourFormWizard, self).__call__( request, *args, **kwargs) def get_form(self, step, data=None): """Override for passing the instance keyword argument.""" # If all your ModelForms aren't of the same model you'll # need to do an isinstance() check, or something similar to # only pass the instance keyword to the right form. return self.form_list[step]( data, prefix=self.prefix_for_step(step), initial=self.initial.get(step, None), instance=self.instance) Use like so:: def some_edit_view(request, some_id): instance = get_object_or_4o4( YourModel, user=request.user, id=some_id) return YourFormWizard([YourForm1, YourForm2])( request, instance=instance) If that's more confusing than helpful, post your code and I'll give you a more specific example. :) - whiteinge .. [1] http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method .. [2] http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#creating-a-formwizard-class --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---