python - Updateview with dynamic form_class -
i dynamically change form_class
of updateview
cbv in django 1.6.
i've tried using get_context_data(), didn't since form initialized. need happen during __init__
, guess. here's i've tried on __init__
:
class updatepersonview(generic.updateview): model = person form_class = "" def __init__(self, *args, **kwargs): super(updatepersonview, self).__init__(*args, **kwargs) person = person.objects.get(id=self.get_object().id) if not person.somefield: self.form_class = oneformclass elif person.somefield: self.form_class = someotherformclass
i'm stuck 'updatepersonview' object has no attribute 'kwargs'
error message when executing person = person.objects.get(id=self.get_object().id)
.
when manually specifying id (e.g. id=9
), setup works.
how can args/kwargs inside init method i'm overriding? particularly need access pk
.
you should override get_form_class
.
(also i'm not sure why you're querying person
: object same self.get_object()
already, there's no point getting id of querying again.)
def get_form_class(self): if self.object.somefield: return oneformclass else: return someotherformclass
Comments
Post a Comment