【问题标题】:'dict' object has no attribute 'create' / 'save'“dict”对象没有属性“create”/“save”
【发布时间】:2012-07-30 18:49:15
【问题描述】:

我尝试将 django poll 应用程序应用到我自己的应用程序(一个 IMC 计算器)中,以便为我在 python/django 中的第一步改进和构建一个小项目。

我尝试保存 accueil.html 上的第一个表单的数据:

<form action="" method="post">
  <table>
    {{ form.as_table }}
  </table>

 <input type ="submit" value="Submit">
</form>

这是我的 forms.py

from django import forms

class IndividuDataForm(forms.Form):
    nom = forms.CharField(max_length=100)
    prenom = forms.CharField(max_length= 100)
    anniversaire = forms.DateField()
    taille = forms.IntegerField()
    poids = forms.IntegerField()

这是我的 models.py

from django.db import models

from django.forms import ModelForm

class Individu(models.Model):

    nom = models.CharField(max_length=100)
    prenom = models.CharField(max_length= 100)
    anniversaire = models.DateField()
    taille = models.IntegerField()
    poids = models.IntegerField()

    def __unicode__(self):

       return self.nom

class ImcResultat(models.Model):

    individu = models.ManyToManyField(Individu)

    imc = models.IntegerField()

    date_imc = models.DateField()

class IndividuForm(ModelForm):

    class Meta:

        model = Individu


class ImcResultatForm(ModelForm):

    class Meta:

        model = ImcResultat

最后我的 *views.py* 在这里: 当我尝试将数据从表单保存到数据库时,问题出在第 13 行 * a = cd.create() *

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from models import Individu, ImcResultat
from forms import IndividuDataForm

def accueil(request):

    if request.method == 'POST':

        form = IndividuDataForm(request.POST)

        if form.is_valid():

            cd = form.cleaned_data

            a = cd.create() **#this is the problem**

            return HttpResponseRedirect('/accueil/page2/')

    else:

        form = IndividuDataForm()

        return render_to_response('accueil.html', {'form':form})





def page2(request):



    return render_to_response('page2.html')

我猜是在 views.py 的第一部分,我的功能不能正常工作,它没有将表单保存在数据库中。 def accueil(请求):

if request.method == 'POST':

    form = IndividuDataForm(request.POST)

    if form.is_valid():

        cd = form.cleaned_data

        a = cd.create() **#this is the problem**

        return HttpResponseRedirect('/accueil/page2/')

检查表单是否有效后,我清理表单中的数据,然后尝试保存。 我得到了这个错误:

Exception Type: AttributeError 
Exception Value: 'dict' object has no attribute 'create'

我猜它来自字典,但在我的代码中我从来没有使用 { } 作为字典列表。 所以我一无所知,陷入了学习的过程中。

感谢您的帮助,我希望从我的第一篇 stackoverflow 帖子中我没有犯太多错误。

【问题讨论】:

    标签: python django django-models django-forms


    【解决方案1】:

    丹尼尔的回答更正确

    if form.is_valid():
        form.save()
    

    我会留下我原来的答案,只是为了文档链接。

    form.cleaned_data 是字典类型,没有 create() 方法。我假设您正在尝试为该数据创建一个模型到您的数据库中?

    查看Can a dictionary be passed to django models on create? 以根据该字典创建模型。一个例子可能是:

    cd = form.cleaned_data
    themodel = Individu(**cd)
    themodel.save()
    

    这里也是处理表单数据的文档。

    https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form

    祝你好运!

    【讨论】:

      【解决方案2】:

      您应该使用 ModelForm,它会自动定义字段以匹配模型上的字段。然后你可以调用form.save()直接从表单创建实例。

      【讨论】:

      • 感谢大家的帮助,再次查看与 ModelForm 一起使用的代码,我使用的是 forms.py 中的 IndividuDataForm,我使用 models.py 中的 IndividuForm 对其进行了更改,它可以工作!!!
      【解决方案3】:

      如果你只想将提交的数据保存为 django ModelForm,使用 form.save()。

      见:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#s-the-save-method

      这将使用已清理/验证的数据。

      如果你需要做某事,你可以使用 form.cleaned_data。除了用模型实例保存它。

      【讨论】:

      • -1:Daniel 或 Ryan 的回答没有涵盖您的哪一部分?
      猜你喜欢
      • 2014-12-14
      • 2018-12-15
      • 1970-01-01
      • 2017-03-30
      • 2012-01-12
      • 2021-03-08
      • 2017-05-12
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多