0

谁能帮我弄清楚如何保存我的 django FormWizard?我已经搜索并阅读了几天的文档,但无法弄清楚为什么我的向导不会保存。

形式:

from django.contrib.formtools.wizard.views import SessionWizardView
from django.core.files.storage import FileSystemStorage

class StepOneForm(forms.Form):
    year = forms.ChoiceField(choices=YEAR_CHOICES)
    ....

class StepTwoForm(forms.Form):
    main_image = forms.ImageField()
    ....

class CreateWizard(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT))
    def done(self, form_list, **kwargs):
        instance = MyModel()
        for form in form_list:
            for field, value in form.cleaned_data.iteritems():
                setattr(instance, field, value)
        instance.save()
        return render_to_response('wizard-done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

楷模:

class MyModel(models.Model):
    user = models.ForeignKey(User)
    year = models.IntegerField(('year'), max_length=4, choices=YEAR_CHOICES, default=datetime.datetime.now().year)
    main_image = models.ImageField(upload_to='photos/main/')
    .....

    def save(self, **kwargs):
        slug = '%s %s %s %s' % (self.user, self.year, self.model, self.series)
        unique_slugify(self, slug)
        super(MyModel, self).save()
    def __unicode__(self):
        return '%s %s' % (self.user, self.year)

回溯表明该错误来自我的模型,我在其中覆盖了 save 方法以创建 slug。这在我使用 . 时工作正常ModelForm,但似乎导致FormWizard.

Traceback:
File "/lib/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/lib/python2.7/django/views/generic/base.py" in view
  48.             return self.dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in dispatch
  223.         response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/views/generic/base.py" in dispatch
  69.         return handler(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in post
  286.                 return self.render_done(form, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in render_done
  328.         done_response = self.done(final_form_list, **kwargs)
File "/myproject/myproject/forms.py" in done
  55.         instance.save()
File "/myproject/myproject/models.py" in save
  95.         slug = '%s %s %s %s' % (self.user, self.year, self.model, self.series)
File "/lib/python2.7/django/db/models/fields/related.py" in __get__
  343.                 raise self.field.rel.to.DoesNotExist

Exception Type: DoesNotExist at /wizard/
Exception Value: 

你能帮我弄清楚如何保存向导,或者如何解决这个错误吗?谢谢你的想法!


编辑:在实施@Rohan 的建议之后。

Traceback:
File "/lib/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/lib/python2.7/django/views/generic/base.py" in view
  48.             return self.dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in dispatch
  223.         response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/views/generic/base.py" in dispatch
  69.         return handler(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in post
  286.                 return self.render_done(form, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in render_done
  328.         done_response = self.done(final_form_list, **kwargs)
File "/myproject/myproject/forms.py" in done
  56.         instance.save()
File "/myproject/myproject/models.py" in save
  97.         super(MyModel, self).save()
File "/lib/python2.7/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/lib/python2.7/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/lib/python2.7/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/lib/python2.7/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/lib/python2.7/django/db/models/sql/compiler.py" in execute_sql
  910.             cursor.execute(sql, params)

Exception Type: InternalError at /wizard/
Exception Value: current transaction is aborted, commands ignored until end of transaction block
4

1 回答 1

0

我认为最有可能self.usersave()方法是给您带来问题。当您尝试创建实例然后保存它时,它没有被设置。

在您的done()方法中,您需要instance.user在保存之前设置为预期值。

于 2013-02-11T04:43:18.867 回答