Rohit Chopra Rohit Chopra - 3 months ago 20
Python Question

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

models.py

class answers(models.Model):
username = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.ForeignKey(Task)
answer = models.URLField()
ANSWER_CHOICES = (
('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


views.py

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 = form.save(commit=False)
instance.save()
context = {
'form':form,
'task': task,
}

**forms.py**
from django import forms
from .models import answers

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


TRACEBACK

Traceback:

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

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

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

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

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

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

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

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\query.py" 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\query.py" 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\query.py" 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\query.py" in build_filter
1183. condition = lookup_class(lhs, value)

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

File "C:\Users\rohit\Desktop\asad\lib\site-packages\django\db\models\fields\related_lookups.py" 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\__init__.py" 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)
and
Task.objects.get(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

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 Task.id field (assuming that that is the primary key field for that type).