如何排除db Django中的项目 [英] How can exclude the item which is in db Django
问题描述
我有两个问题
-
我只想排除db中的项目,否则将显示ordeerRequest表中没有的其他项目,但是在我查询后却什么也没显示
I just want to exclude the items which is in db, other item which is not in ordeerRequest table will be display, but after my query it show nothing
在模板中,当我选择任何类别时,它只会显示我添加的最后一个类别.选择任何类别后,它应显示该类别,但默认情况下将显示最后添加的类别
In template when i select any category it just show the last category which i added. After select any category it should show the that category but it show last added category by default
View.py
def get(self, request, *args, **kwargs):
status = self.request.GET.get('action') == 'accept'
orderCheck = Order.objects.exclude(
orderrequest__order_status__in=OrderRequest.objects.filter(order_status=status)
)
args = {'orderCheck': orderCheck}
return render(request, self.template_name, args)
类别展示
def get(self, request, *args, **kwargs):
categories = Category.objects.all()
categoryId = self.request.GET.get('SelectCategory')
products = Product.objects.filter(category_id=categoryId)
args = {'categories': categories, 'products': products, 'selectedCategory': categoryId}
return render(request, self.template_name, args)
模板
<label>
<select name="SelectCategory" >
<option disabled="disabled" {{ selectedCategory|yesno:"yeah, no" }}> Select Category</option>
{% for category in categories %}
<option value="{{ category.id }}" {% if category.id == selectedCategory %} selected{% endif %} >
{{ category.name }}
</option>
{% endfor %}
</select>
</label>
推荐答案
看起来您过于复杂了,只是想要:
Looks like you overcomplicated it a bit and just want:
Order.objects.exclude(orderrequest__order_status=status)
现在,您尝试排除 Order
记录,其中 Order.orderrequest.order_status
位于 OrderRequest
对象,因为它永远都不会,所以不会排除任何东西.
Right now you're trying to exclude Order
records where Order.orderrequest.order_status
is in a QuerySet
of OrderRequest
objects, which it never will be, so nothing is being excluded.
关于您问题的第二部分,我从您之前发布的问题中意识到了这一点.您仍然在所有< option>
标签中包含了 selected
.如果根本没有单词" selected
",则该项目将被视为" selected
",即使它说的是" selected =" please God no"
.将< option>
标记为选中的正确方法是:
As to the second part of your question, I recognize this from an earlier question you posted. You've still included selected
in all of your <option>
tags. If the word selected
is in there at all, the item will be treated as selected
, even if it says selected="please God no"
. The proper way to mark an <option>
as selected is:
< option value ="some_value"已选择>
这篇关于如何排除db Django中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!