比方說,我有三個型號:對象過濾器 - 需要提醒
class ONE (models.Model):
[some awesome stuff]
class TWO (models.Model):
one = models.ForeignKey(ONE)
user = models.ForeignKey(User)
[other stuff]
class THREE (models.Model):
one = models.ForeignKey(ONE)
[stuff and whatnot]
的看法目前看起來是這樣的:
def my_view(request, username, template_name='view.html'):
current_user = get_object_or_404(User, username=username)
two = TWO.objects.filter(user_id=current_user)
three = THREE.objects.filter(one_id=two)
data = {
'user': current_user,
'two': two,
'three': three
}
return render(request, template_name, data)
數據庫條目(它被簡化;有更多的數據在每個表中):
Table ONE
----------
id=1
id=2
id=3
Table TWO
----------
one_id=1; user_id=1
one_id=2; user_id=1
one_id=3; user_id=1
Table THREE
----------
one_id=1
one_id=2
one_id=3
有了上述觀點,我不能夠顯示錶THREE中來自表TWO的user_id = 1的所有條目。
錯誤消息= more than one row returned by a subquery used as an expression
我試圖用three = THREE.objects.filter(one_id__in=two)
但不顯示HTML頁面上的任何內容。沒有錯誤信息。
HTML:
{% for two in two %}
<p>{{ two.one }}</p> <!-- that works! -->
{% endfor %}
{% for three in three %}
<ul>
<li>{{ three }}</li>
</ul>
{% endfor %}
是否有我需要的過濾器選項?
有什麼'response'變量樣子? –
這是一個錯誤。 'response' =='two'。修復。 –