Django:无法将表单数据保存到数据库

时间:2018-08-05 00:36:06

标签: python django django-models django-forms django-views

我有一个用于注册用户的多步骤表单/向导。但是,我无法保存数据。我在整个代码中给出了各种打印命令,以调试运行时发生的情况。打印语句显示一切似乎都正常。但是,我是用空值在数据库中创建的空对象。下面是代码片段:

views.py

from django.shortcuts import render, render_to_response
from .models import User, Password, PersonalDetails, CCDetails, RoundUpDetails, construct_instance
#from .models import User
from .forms import PostForm
from formtools.wizard.views import SessionWizardView
from django.core.mail import send_mail

class ContactWizard(SessionWizardView):
    template_name = 'Done.html'

    def done(self, form_list, form_dict, **kwargs):

        form_data = process_form_data(form_list)

        return render('home.html', {form_data: 'form_data'})

def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]
    data = [User, Password, PersonalDetails, CCDetails, RoundUpDetails]
    for i, x in enumerate(form_data):
        print("value of x: ", x)
        inst = data[i]
        newObject = inst()
        print ("BnewObject", newObject)

        for k, v in x.items():
            print("value of key: ", k)
            print("value of value: ", v)
            newObject.k = v
            print("newObject.k:", newObject.k)

        newObject.save()

        print("AnewObject", newObject)

输出:

value of x:  {'email': 'as@gmail.com'}
BnewObject User object ()
value of key:  email
value of value:  as@gmail.com
newObject.k: as@gmail.com
AnewObject User object ()
value of x:  {'user': 'po', 'password': 'po', 'confirmPassword': 'po'}
BnewObject Password object (None)
value of key:  user
value of value:  po
newObject.k: po
value of key:  password
value of value:  po
newObject.k: po
value of key:  confirmPassword
value of value:  po
newObject.k: po
AnewObject Password object (12)
value of x:  {'firstName': 'hjh', 'lastName': 'jhjhjh', 'phone': 'ghgjg', 'address': 'ggjj'}
BnewObject PersonalDetails object (None)
value of key:  firstName
value of value:  hjh
newObject.k: hjh
value of key:  lastName
value of value:  jhjhjh
newObject.k: jhjhjh
value of key:  phone
value of value:  ghgjg
newObject.k: ghgjg
value of key:  address
value of value:  ggjj
newObject.k: ggjj
AnewObject PersonalDetails object (12)
value of x:  {'ccNum': '4386280056785678', 'month': datetime.date(2018, 8, 31), 'cvvNum': '123'}
BnewObject CCDetails object (None)
value of key:  ccNum
value of value:  4386280056785678
newObject.k: 4386280056785678
value of key:  month
value of value:  2018-08-31
newObject.k: 2018-08-31
value of key:  cvvNum
value of value:  123
newObject.k: 123
AnewObject CCDetails object (12)
value of x:  {'cause': 'education', 'charity': 'a', 'zipCode': 'hghg'}
BnewObject RoundUpDetails object (None)
value of key:  cause
value of value:  education
newObject.k: education
value of key:  charity
value of value:  a
newObject.k: a
value of key:  zipCode
value of value:  hghg
newObject.k: hghg
AnewObject RoundUpDetails object (12)

如您所见,输出显示已创建对象并分配了正确的值。但是,我看不到它们保存在数据库中。请帮忙!

PS:我正在使用MySql数据库。

1 个答案:

答案 0 :(得分:2)

尝试用setattr(newObject, k, v)代替newObject.k = v,就像用点表示法一样,将属性k(不是基础字符串值)分配给newObject。要以这种方式设置属性,应使用getattr(newObject, k)而不是newObject.k