我在论坛上发现了类似的错误,但是我没有看到我的代码中有任何错误,模型和视图实际上并不复杂。
class Word(models.Model):
word_text = models.CharField(max_length=50)
user_crea = models.CharField(max_length=20)
publ_date = models.DateTimeField(auto_now_add=True)
modi_date = models.DateTimeField(auto_now=True)
class Explanation(models.Model):
word_foid = models.ForeignKey(Word, on_delete=models.CASCADE)
expl_text = models.CharField(max_length=50)
user_crea = models.CharField(max_length=20)
publ_date = models.DateTimeField(auto_now_add=True)
modi_date = models.DateTimeField(auto_now=True)所以在我看来,对外关系应该是好的。当我通过管理面板添加新数据时,我没有问题。我在视野中使用了它:
views.py
def index(request):
if request.method == 'POST':
form = Search(request.POST)
if form.is_valid():
dane = form.cleaned_data
tlumaczenie=Explanation.objects.filter(Word__word_text=dane['Searched_word'])
print(tlumaczenie)
return render(request,'explanation.html', {'dane':dane})但我还是会犯错误:
django.core.exceptions.FieldError:无法将关键字“Word”解析为字段。选择: expl_text,id,modi_date,publ_date,user_crea,word_foid,word_foid_id
我不明白为什么。它应该发送如下的查询:
select e.* from word w join explanation e on w.word_id = e.word_foid where w.word_text = dane['Searched_word'] 并取回数据。
知道它为什么不能正常工作吗?
发布于 2017-06-06 21:43:53
您的外键字段名为word_foid,而不是Word。查询应该像这样使用这个名称
tlumaczenie = Explanation.objects.filter(word_foid__word_text=dane['Searched_word']) https://stackoverflow.com/questions/44400251
复制相似问题