0
models.pyとValueError: 'ABC'
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'
ベース10と、INT()に対して無効リテラル
私は持っていますタスクモデルの外部キーとして使用されるタイトル。 get_object_or_404(answers, title=task.title)
とのようなクエリを使用しているとき、それは私にこのトレースバックを与えています。よく私はこのエラーを取り除く方法を知らない。そしてタイトルが外国の分野であることを私が理解できないことは、どうすればこの外部キーを使ってデータを得ることができますか?
NOT NULL制約に失敗しました:answers_answers.title_id'これで入力を提出するとこのエラーが表示されます。 –
エラーが 'instance.save()'にあります –
@RohitChopra: 'Task.objects.get()'は 'None'を返しました。おそらく、あなたは 'Task.objects.get(id = id)'の代わりに 'get_object_or_404(Task、id = id)'を使いたいでしょうか? –