任何人都可以帮我弄清楚如何保存我的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:
您能帮我弄清楚如何保存向导,或者如何解决此错误?谢谢你的想法!
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
答案 0 :(得分:0)
我认为最有可能self.user
save()
方法给你的问题。当您尝试创建实例然后保存它时,它不会被设置。
在done()
方法中,您需要在保存之前将instance.user
设置为预期值。