A
A
Artyom Innokentiev2016-01-16 21:43:43
Django
Artyom Innokentiev, 2016-01-16 21:43:43

Why can't I update model fields?

forms.py:

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo
        exclude = ['created']

views.py:
class FooView(FormView):
    success_url = '/some/url/'
    template_name = 'some/template.html'
    form_class = FooForm
    model = Foo

    def form_valid(self, form):
        if 'foo' in self.request.session:
            pk = self.request.session['foo']
            foo = Foo.objects.get(pk=pk)
            self.object = form.save(instance=foo)
        else:
            self.object = form.save()
            self.request.session['foo'] = self.object.pk
        return HttpResponseRedirect(self.get_success_url())

Throws an error when trying to update the model via save(instance=foo):
TypeError at /some/url
save() got an unexpected keyword argument 'instance'

What is the problem?
Django 1.9.0
Python 3.4.3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-01-16
@artinnok

https://docs.djangoproject.com/en/1.9/topics/forms...

def form_valid(self, form):
        if 'foo' in self.request.session:
            pk = self.request.session['foo']
            foo = Foo.objects.get(pk=pk)
            TheForm = self.get_form_class()
            self.object = TheForm(instance=foo)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question