ValueError:基数为10的int()的无效文字: - CharField

时间:2016-08-16 15:24:21

标签: python django

我试图用模型将对象添加到我的数据库中。但是得到一个错误。据我所知,CharField是字符串?错误抱怨它是一个无效的整数?

from django.shortcuts import HttpResponse
from Cr.models import User

def index(request):

    a = User('Ra', 'sen')
    a.save()
    print(a.username)

    return HttpResponse('<h1>Hello World!</h1>')

模型文件

from django.db import models

class User(models.Model):
    username = models.CharField(max_length=250)
    password = models.CharField(max_length=100)

控制台打印:

Internal Server Error: /Crowd/
Traceback (most recent call last):
  File "C:\Anaconda3\Lib\site-packages\django\core\handlers\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Anaconda3\Lib\site-packages\django\core\handlers\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 7, in index
    a.save()
  File "C:\Anaconda3\Lib\site-packages\django\db\models\base.py", line 708, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\base.py", line 736, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\base.py", line 801, in _save_table
    forced_update)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\base.py", line 831, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\query.py", line 1203, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\query.py", line 1099, in build_lookup
    return final_lookup(lhs, rhs)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\lookups.py", line 19, in __init__
    self.rhs = self.get_prep_lookup()
  File "C:\Anaconda3\Lib\site-packages\django\db\models\lookups.py", line 57, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\fields\__init__.py", line 744, in get_prep_lookup
    return self.get_prep_value(value)
  File "C:\Anaconda3\Lib\site-packages\django\db\models\fields\__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'Ra'
[16/Aug/2016 17:21:21] "GET /Crowd/ HTTP/1.1" 500 134020

1 个答案:

答案 0 :(得分:3)

a = User('Ra', 'sen')

第一个位置参数用于pk。创建模型时,您应该使用关键字参数:

a = User(username='Ra', password='sen')