vandelay vandelay - 1 year ago 84
Python Question

ValueError: invalid literal for int() with base 10: - CharField

I'm trying to add an object to my database with models. But getting an error. As I understand

are strings? And the error complains that it's an invalid integer?

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

def index(request):

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

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

Models file

from django.db import models

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

console print:

Internal Server Error: /Crowd/
Traceback (most recent call last):
File "C:\Anaconda3\Lib\site-packages\django\core\handlers\", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Anaconda3\Lib\site-packages\django\core\handlers\", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rasmus\workspace\Crowd\src\Cr\", line 7, in index
File "C:\Anaconda3\Lib\site-packages\django\db\models\", line 708, in save
force_update=force_update, update_fields=update_fields)
File "C:\Anaconda3\Lib\site-packages\django\db\models\", 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\", line 801, in _save_table
File "C:\Anaconda3\Lib\site-packages\django\db\models\", line 831, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "C:\Anaconda3\Lib\site-packages\django\db\models\", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Anaconda3\Lib\site-packages\django\db\models\", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\", line 1203, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Anaconda3\Lib\site-packages\django\db\models\sql\", line 1099, in build_lookup
return final_lookup(lhs, rhs)
File "C:\Anaconda3\Lib\site-packages\django\db\models\", line 19, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Anaconda3\Lib\site-packages\django\db\models\", 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\", line 744, in get_prep_lookup
return self.get_prep_value(value)
File "C:\Anaconda3\Lib\site-packages\django\db\models\fields\", 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

Answer Source

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

The first positional argument is used for the pk. When creating a model you should use keyword arguments:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download