如果至少有一个字段包含此名称,我想过滤对象并获取结果。
models.py:
class Item(BaseModel):
name = models.CharField(max_length=255)
seller = models.ForeignKey(Company)
quantity = models.FloatField(default=0.0)
我试过这个,但它不起作用。
views.py:
search_filter_kw = {}
item_name = request.GET.get("searchitemname", '')
if item_name != '':
search_filter_kw['Q(name__contains) | Q(seller__name__contains)'] = item_name
答案 0 :(得分:4)
Item.objects.filter(Q(name__icontains=item_name) | Q(seller__name__icontains=item_name))