如何在django的外国基领域运行_icontains方法
我正在建立一个虚拟的电子商务网站。为此,我希望构建一个员工门户网站,在其中我可以通过用户的名字来搜索待处理的订单。我已经在我的模型上制作了一个外国钥匙领域。但是,当我在用户字段上使用__icontains方法时,我会得到相关字段的查找无效:icontains
错误。我希望返回所有用用户以其名字搜索的字符串的用户下达的订单。
我的Views.py:
class delivery_staff_search_pending_orders(LoginRequiredMixin, generic.View):
def get(self, *args, **kwargs):
if self.request.user.groups.filter(name='Delivery_staff_admin').exists():
search_query = self.request.GET['query']
filter_form = PendingOrdersFilterForm()
orders = Order.objects.filter(preproccessed=True, ordered=True, delivered=False)
searched_pending_orders = orders.filter(user__icontains=search_query)
context = {
"filter_form": filter_form,
"pending_orders_list": searched_pending_orders
}
return render(self.request, "staff/delivery/pending_orders.html", context)
我的订单模型:
user = models.ForeignKey(User, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
ref_code = models.CharField(max_length=20, default=1234)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
billing_address = models.ForeignKey("BillingAddress", on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey("Coupon", on_delete=models.SET_NULL, blank=True, null=True)
preproccessed = models.BooleanField(default=False)
dispatched = models.BooleanField(default=False)
delivered = models.BooleanField(default=False)
Refund_requested = models.BooleanField(default=False)
Refund_granted = models.BooleanField(default=False)
Paid_price = models.FloatField(default=0)
用户模型是标准django.contrib.auth.models用户模型
I am building a dummy E-commerce site. For this, I wish to build a staff portal where I can search for pending orders by a user's first name. I have made a foreignkey field on my model that I am referencing. But when I use the __icontains method in my views on user field, I get Related Field got invalid lookup: icontains
error. I wish to return all orders that have been made by a user that have a certain searched string in their first name.
My views.py:
class delivery_staff_search_pending_orders(LoginRequiredMixin, generic.View):
def get(self, *args, **kwargs):
if self.request.user.groups.filter(name='Delivery_staff_admin').exists():
search_query = self.request.GET['query']
filter_form = PendingOrdersFilterForm()
orders = Order.objects.filter(preproccessed=True, ordered=True, delivered=False)
searched_pending_orders = orders.filter(user__icontains=search_query)
context = {
"filter_form": filter_form,
"pending_orders_list": searched_pending_orders
}
return render(self.request, "staff/delivery/pending_orders.html", context)
My Order model:
user = models.ForeignKey(User, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
ref_code = models.CharField(max_length=20, default=1234)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
billing_address = models.ForeignKey("BillingAddress", on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey("Coupon", on_delete=models.SET_NULL, blank=True, null=True)
preproccessed = models.BooleanField(default=False)
dispatched = models.BooleanField(default=False)
delivered = models.BooleanField(default=False)
Refund_requested = models.BooleanField(default=False)
Refund_granted = models.BooleanField(default=False)
Paid_price = models.FloatField(default=0)
The user model is the standard django.contrib.auth.models user model
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好
icontains
与关系无法使用,而是与相关模型的字段:将完美工作。
Well
icontains
doesn't work with relationships but with field of the related model:Will works perfectly.