数据插入错误:带有基数10的int()的文字无效

时间:2010-11-15 11:06:39

标签: python django google-app-engine django-nonrel

我在Google App Engine上使用Django-nonrel。我正在尝试向数据库添加一行,但在尝试使用save()时出现此错误:

  

对于具有基数10的int()的无效文字

这是我的代码:

views.py

from django import forms
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from forms import SayForm
from models import Saying, Category
import datetime

def say_something(request):
if request.method == 'POST':
    form = SayForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        content = cd['content']
        category_temp = "Uncategorized"
        category = Category.objects.get(name = category_temp)
        added_date = datetime.datetime.now()
        added_user = request.user
        saying = Saying(content, category, added_date, added_user)
        saying.save()
        return HttpResponseRedirect('/contribute/success')
else:
    form = SayForm()
return render_to_response('say_form.html', {'form' : form})

models.py

from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    name = models.CharField(max_length = 50)

    def __unicode__(self):
        return self.name

class Saying(models.Model):
    content = models.CharField(max_length = 160)
    category = models.ForeignKey(Category)
    added_date = models.DateField()
    added_user = models.ForeignKey(User)

forms.py

from django import forms

class SayForm(forms.Form):
    content = forms.CharField(widget = forms.Textarea)

    def clean_message(self):
        content = self.cleaned_data['content']
        num_characters = len(content)
        if num_characters > 160:
            raise forms.ValidationError("Please limit your saying to 160 characters only.")
        num_words = len(content.split())
        if num_words < 4:
            raise forms.ValidationError("This doesn't make sense. Say something longer.")
        return content

编辑:这是回溯

  

回溯:文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/core/handlers/base.py”   在get_response中     107.响应=回叫(请求,   * callback_args,** callback_kwargs)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/contrib/auth/decorators.py”   在_wrapped_view中     25. return view_func(request,* args,** kwargs)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/core/views.py”   在say_something     36. say.save()

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/base.py”   在保存     452. self.save_base(using = using,   force_insert = force_insert,   force_update = force_update)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/base.py”   在save_base中     550.对于meta.local_fields中的f]

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/fields/subclassing.py”   在内心     28. return func(* args,** kwargs)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/fields/subclassing.py”   在内心     28. return func(* args,** kwargs)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/fields/init.py”   在get_db_prep_save中     280. return self.get_db_prep_value(value,   connection = connection,prepared = False)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/fields/subclassing.py”   在内心     53. return func(* args,** kwargs)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/models/fields/init.py”   在get_db_prep_value中     492. return connection.ops.value_to_db_auto(value)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/djangotoolbox/db/base.py”   在value_to_db_auto中     68. return super(NonrelDatabaseOperations,   自).value_to_db_auto(值)

     

文件   “/home/eeyorexd/workspace/Python/appengine/something-to-say/somethingtosay/django/db/backends/init.py”   在value_to_db_auto中     485. return int(value)

     

异常类型:ValueError at   / contrib / Exception Value:无效   对于具有基数10的int()的文字:'test'

我的直觉告诉我,问题在于如何将对象保存到数据库。也许外键部分?我最近刚刚开始学习Django,因此无法确定问题所在。这个问题与使用GAE后端的Django-nonrel有什么关系吗?谁能告诉我这里哪里出错?

2 个答案:

答案 0 :(得分:5)

问题在于:

saying = Saying(content, category, added_date, added_user)

你忘记了Django在模型定义中添加了一个自动id字段。如果您在外壳中执行此操作,然后打印saying.__dict__,您会看到内容已分配到id,类别分配到content,依此类推。

相反,在实例化模型时始终使用关键字参数:

saying = Saying(content=content, 
                category=category, 
                added_date=added_date, 
                added_user=added_user)

答案 1 :(得分:3)

创建这样的实例会将模型的主键设置为content

saying = Saying(content, category, added_date, added_user)

哪个不起作用,因为您的模型具有数字主键,默认情况下所有模型都是如此,除非您明确告诉它使用其他字段作为主键。当Django试图在值上调用int()以将其强制转换为整数时,它会因类型错误而崩溃。

您应该使用关键字参数,如下所示:

saying = Saying(content = content, ... = ...)

执行此操作的惯用方法,因为您仍想保存对象:

saying = Saying.objects.create(content = content, ... = ..)