[英]how can I filter the difference between the two models? using django
I have 2 models, Product and ProductRelatedGroupAndProduct , the Product have 5 data, [chicken, okra, ampalaya, cabbage] and the ProductRelatedGroupAndProduct have [chicken, okra, cabbage] how do i get the remaining data in Product that dont have in ProductRelatedGroupAndProduct ?我有 2 个模型, Product和ProductRelatedGroupAndProduct , Product有 5 个数据,[chicken, okra, ampalaya, cabbage] 和ProductRelatedGroupAndProduct有 [chicken, okra, cabbage] 如何获取Product中没有的剩余数据ProductRelatedGroupAndProduct ?
the choosen product is ProductRelatedGroupAndProduct and the list of product is Product选择的产品是ProductRelatedGroupAndProduct ,产品列表是Product
I just want that the data displayed on the choosen product should no longer be visible in the product list.我只想让所选产品上显示的数据不再显示在产品列表中。
so far this is my views.py到目前为止,这是我的 views.py
def searchrelatedproduct(request):
id = request.GET.get('relatedproductID')
products = Product.objects.all()
relatedproduct = ProductRelatedGroupAndProduct.objects.filter(productrelatedgroup = id)
return render(request, "customAdmin/relatedproduct.html",{"products":products,"relatedproduct":relatedproduct})
this is my html这是我的 html
<div class="container">
<form method="POST" action="/GroupOfProduct/" enctype="multipart/form-data">{% csrf_token %}
<h2>Product </h2>
<div class="form-group">
<br>
<label for="sel2">List Of Products</label>
<select multiple class="form-control" id="sel2" name="product">
{% for product in products %}
<option>{{product.product}}</option>
{% endfor %}
</select>
</div>
<input type="submit" style="float:right;">
</form>
</div>
<div class="container">
<h2>Choosen Product</h2>
<form method="POST" action="/UpdateGroupOfProduct/" enctype="multipart/form-data">{% csrf_token %}
<input type="submit" style="float:right;">
<div class="form-group">
<br>
<label for="sel2">List Of Choosen Products</label>
<select multiple class="form-control" id="sel2" name="relatedproduct">
{% for product in relatedproduct %}
<option value="{{ product.id }}">{{product.product}}</option>
{% endfor %}
</select>
</div>
</form>
</div>
this is my models.py这是我的模型.py
class ProductRelatedGroupAndProduct(models.Model):
product = models.ForeignKey(Product,on_delete=models.SET_NULL, null=True,blank=True,verbose_name="Product")
productrelatedgroup = models.ForeignKey('ProductGroup',
on_delete=models.SET_NULL, null=True, blank=True,
verbose_name="ProductGroup")
class Product(models.Model):
product = models.CharField(max_length=500)
class ProductGroup(models.Model):
category = models.CharField(max_length=500, blank=True)
UPDATE更新
when i tried this当我尝试这个
diffproducts = products.exclude(pk__in=relatedproduct)
print(diffproducts)
it prints all data of the Product它打印产品的所有数据
<QuerySet [<Product: cabbage>, <Product: ampalaya>, <Product: okra>, <Product: Chicken>]>
and when i tried this当我尝试这个时
unused_product = set(relatedproduct).difference(set(products))
print(unused_product)
***result***
{<ProductRelatedGroupAndProduct: cabbage>, <ProductRelatedGroupAndProduct: ampalaya>, <ProductRelatedGroupAndProduct: okra>}
and this还有这个
Product.objects.filter(productrelatedgroupandproduct__isnull=True)
i get this error我收到这个错误
django.core.exceptions.FieldError: Cannot resolve keyword 'productrelatedgroupandproduct' into field
You can use reverse relation for filter:您可以对过滤器使用反向关系:
Product.objects.filter(productrelatedgroupandproduct__isnull=True)
productrelatedgroupandproduct
here is a lowercased name of ProductRelatedGroupAndProduct
model. It's default value for related_query_name argument.此处的productrelatedgroupandproduct
是ProductRelatedGroupAndProduct
model的小写名称。它是 related_query_name 参数的默认值。
This could be possible by converting your queryset to a set and then use intersection
for complex cases.这可以通过将查询集转换为集合然后对复杂情况使用intersection
来实现。
# get objects present in both
set(products).intersection(set(relatedproduct))
# get product not in relatedproduct
set(products).difference(set(relatedproduct))
# opposite
set(relatedproduct).difference(set(products))
In your case exclude
can do the trick: products.exclude(pk__in=relatedproduct)
在您的情况下, exclude
可以解决问题: products.exclude(pk__in=relatedproduct)
You can use the exclude function:您可以使用排除 function:
views.py:意见.py:
def searchrelatedproduct(request):
id = request.GET.get('relatedproductID')
products = Product.objects.all()
relatedproduct = ProductRelatedGroupAndProduct.objects.filter(pk=id)
diffproducts = products.exclude(pk__in=relatedproduct)
return render(request, "test.html",{"products":diffproducts,"relatedproduct":relatedproduct})
and output in template in your orignal html.和原始 html 中的模板中的 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.