Rohit Chopra Rohit Chopra - 1 year ago 175
Python Question

ValueError: invalid literal for int() with base 10: 'abc'

class answers(models.Model):
username = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.ForeignKey(Task)
answer = models.URLField()
('F', 'Declined'),
('T', 'Accepted'),
accept_answer = models.CharField(max_length=1, choices=ANSWER_CHOICES, default='f')

def __str__(self):
return self.answer

def __unicode__(self):
return self.answer

def full_task(request, id):
task = Task.objects.get(id = id)
instance = get_object_or_404(answers, title=task.title)
form = AnswerForm(request.POST or None)
if form.is_valid():
instance =
context = {
'task': task,

from django import forms
from .models import answers

class AnswerForm(forms.ModelForm):
answer = forms.URLField()
class Meta:
model = answers
fields = [



File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\core\handlers\" in inner
39. response = get_response(request)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\core\handlers\" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\core\handlers\" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\rohit\Desktop\asad\website\user_profile\" in full_task
75. instance = get_object_or_404(answers, title=task.title)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\" in get_object_or_404
85. return queryset.get(*args, **kwargs)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\" in get
376. clone = self.filter(*args, **kwargs)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\" in filter
796. return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\" in _filter_or_exclude
814. clone.query.add_q(Q(*args, **kwargs))

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\sql\" in add_q
1227. clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\sql\" in _add_q
1253. allow_joins=allow_joins, split_subq=split_subq,

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\sql\" in build_filter
1183. condition = lookup_class(lhs, value)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\" in __init__
19. self.rhs = self.get_prep_lookup()

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\fields\" in get_prep_lookup
100. self.rhs = target_field.get_prep_value(self.rhs)

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\fields\" in get_prep_value
946. return int(value)

Exception Type: ValueError at /tasks/1/
Exception Value: invalid literal for int() with base 10: 'abc'

i have used title as foreign key of Task model. and when i am using query like
get_object_or_404(answers, title=task.title)
it is giving me this traceback. well i don't know how to remove this error . and one thing i cant understand that the title is a foreign field then how can i get data from diffrent using this foreign key?

Answer Source

You need to pass in the task instance, not task.title:

instance = get_object_or_404(answers, title=task)

task.title is a Unicode string, but the answers.title field is a foreign key. You could also pass in the field (assuming that that is the primary key field for that type).

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