ValueError:基数为10的int()的无效文字:'abc'

时间:2016-09-09 11:09:44

标签: python django django-models django-views

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'

我使用title作为Task模型的外键。当我使用查询时 get_object_or_404(answers, title=task.title)Task.objects.get(title=task.title)它正在给我这个追溯。以及我不知道如何删除此错误。有一件事我无法理解标题是一个外国字段然后如何使用这个外键从不同的数据获取数据?

1 个答案:

答案 0 :(得分:2)

您需要传递task个实例,而不是task.title

instance = get_object_or_404(answers, title=task)

task.title是Unicode字符串,但answers.title字段是外键。您还可以传入Task.id字段(假设这是该类型的主键字段)。