我有如下问题和QuestionChoices模型,当我尝试从Questionchoices中检索问题和相关答案时,我得到以下错误,说明查询是期待字符串。什么可能是错误的模型/查询?
class Question(models.Model):
Question_Id = models.AutoField(primary_key=True)
Question_Text = models.TextField(max_length=1000)
def __str__(self):
return self.Question_Text
def __int__(self):
return self.Question_Id
class QuestionChoices(models.Model):
Choice_Id = models.AutoField(primary_key=True)
Question_Choices_Question_Id = models.ForeignKey("Question", on_delete=models.CASCADE)
Choice = models.TextField(max_length=500)
Is_Right_Choice = models.BooleanField(default=False)
>>> QuestionChoices.objects.select_related().filter(Question_Choices_Question_Id = Question.Question_Id)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 836, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 854, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1253, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1277, in _add_q
split_subq=split_subq,
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1215, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1085, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\lookups.py", line 18, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\adm\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 947, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
答案 0 :(得分:0)
运行shell命令后出现同样的错误。问题是由代码的filter()设置方式引起的。该参数需要模型对象 OR 外键作为值。
python3 manage.py shell
>>> from my_app.models import *
>>> my_question_obj = Question.objects.create(Question_Text = "This is my question")
>>> QuestionChoices.objects.select_related().filter(Question_Choices_Question_Id=my_model_obj)
或者,如果您想在数据库中筛选出问题,可以使用get()来检索对象。
python3 manage.py shell
>>> from my_app.models import *
>>> my_question_obj = Question.objects.get(Question_Text="This is the text of your question")
>>> QuestionChoices.objects.select_related().filter(Question_Choices_Question_Id = my_model_obj)