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)

トレースバックは、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 に答える