i have form
modelchoicefield
, being used form_class
in formview
.
the choice field has populated information bound request
object.
let's summarize:
class myformview(formview): # need pass `request.user` , value # derived `request.get['pk']` form form_class = myform class myform(form): choices = modelchoicefield(queryset=mychoice.objects.none()) def __init__(self, user, number, *args, **kwargs): super(myform, self).__init__(*args, **kwargs) self.fields['choices'] = mychoice.objects(number=number, owner=user)
what need pass data form when instance created?
i tried overriding get_form
unsure proper way of doing this:
def get_form(self, form_class): user = self.request.user number = somemodel.objects.get(self.get['pk']).number return form_class(user, number, **self.get_form_kwargs())
overriding get_form
work, better approach override get_form_kwargs
, don't have duplicate code get_form
method.
class myformview(formview): form_class = myform def get_form_kwargs(self): kwargs = super(myformview, self).get_form_kwargs() kwargs['user'] = self.request.user kwargs['number'] = somemodel.objects.get(self.get['pk']).number return kwargs
Comments
Post a Comment