forms - Use get_or_create Django -


this continuation of this question.

the problem code not allow creating new objects because of thing = get_object_or_404(thing, pk=id)

class createwizard(sessionwizardview):     file_storage = filesystemstorage(location=os.path.join(settings.media_root))     def done(self, form_list, **kwargs):        id = form_list[0].cleaned_data['id']        thing = get_object_or_404(thing, pk=id)        if thing.user != self.request.user:            raise httpresponseforbidden()        else:            instance = thing()            form in form_list:                field, value in form.cleaned_data.iteritems():                    setattr(instance, field, value)            instance.user = self.request.user            instance.save()            return render_to_response('wizard-done.html', {                'form_data': [form.cleaned_data form in form_list],}) 

how can use get_or_create function? or there another, better way create new objects within function? ideas!

one way be:

class createwizard(sessionwizardview):     file_storage = filesystemstorage(location=os.path.join(settings.media_root))      def done(self, form_list, **kwargs):         id = form_list[0].cleaned_data['id']         try:            thing = thing.objects.get(pk=id)            instance = thing        except:            thing = none            instance = none         if thing , thing.user != self.request.user:            raise httpresponseforbidden()         if not thing:            instance = thing()            form in form_list:                field, value in form.cleaned_data.iteritems():                    setattr(instance, field, value)            instance.user = self.request.user            instance.save()         return render_to_response('wizard-done.html', {                'form_data': [form.cleaned_data form in form_list],}) 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -