0
我在论坛上发现类似的错误,但我没有看到我的代码中有任何错误,模型和视图真的不复杂。Django无法将关键字'Word'解析为字段。选择是:
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: Cannot resolve keyword 'Word' into field. Choices are: 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']
并检索数据。
任何想法为什么它不能正常工作?
但我不知道word_id。当我继续我的观点时,我只有word_text。我想我就像文档显示它一样:“Django提供了一种强大而直观的方式来在查找中”追踪“关系,在后台自动为您处理SQL JOIN。要跨越关系,只需使用字段名称跨模型的相关字段,用双下划线分隔,直到您到达您想要的字段。 本示例使用名为'Beatles Blog'的博客检索所有Entry对象: >>> Entry.objects。过滤器(blog__name ='披头士博客')“ – Artemise
'Explanation.objects.filter(word_foid__word_text = dane ['Searched_word'])'是你要找的东西 – Brobin