Just to rule out the obvious - you're not passing anything to the form constructor. Have you tried it with instance=user_school
? There might be more that needs work but I'd start there.
To expand on this a bit - in your view, you're completely overriding the built in get
method. That's fine, but it means that you're bypassing some of the automated behavior of your view superclass. Specifically, the get
method of ProcessFormView
(one of your ancestor classes) instantiates the form using the get_form
method of the view class. FormMixin
, another ancestor, defines get_form
:
return form_class(**self.get_form_kwargs())
And get_form_kwargs
on ModelFormMixin
adds self.object
to the form's kwargs
:
kwargs.update({'instance': self.object})
Because your overridden get
method does not call get_form
, it also doesn't call get_form_kwargs
and therefore doesn't go through the whole path that provides an initial binding for the form.
I personally would try to handle this by modifying the get_object
method of your custom view and leaving the rest alone:
class EditSchool(UpdateView):
model = School
success_url = reverse_lazy('list_schools')
form_class = UserSchoolForm
def get_object(self, queryset=None):
school = super(EditSchool, self).get_object(queryset=queryset)
user_school, created = UserSchool.objects.get_or_create(
master_school=school, user=self.request.user,
defaults={'name' : school.name, 'mascot' : school.mascot})
return user_school
There may be more changes needed - I haven't tested this - but both the get
and set
methods use get_object
, and bind it to the form as appropriate.