[英]Django: Filter ListView on currently logged-in user (manytomanyfield)
I'm working on a django project, and as I'm quite new to this framework and backend in general, I'm having a hard time finding out how to do this.我正在研究一个 django 项目,由于我对这个框架和后端都很陌生,所以我很难找到如何做到这一点。
I'm building a ListView that is used to display a list of products in a template.我正在构建一个 ListView,用于在模板中显示产品列表。 However, I need it to only display the products made available on the site by the dealer who's currently logged in. So in other words, just a page where a dealer can see what products they've put on our site.
但是,我需要它只显示当前登录的经销商在网站上提供的产品。换句话说,只是一个页面,经销商可以看到他们在我们的网站上放置了哪些产品。
Here's my view:这是我的看法:
class ArticleListView(ListView, LoginRequiredMixin):
template_name = "accounts/list_articles.html"
model = Product
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context["website"] = Website.objects.first()
context["product_list"] = context["product_list"].filter(published=True)
return context
Here's the Products and Dealers models (only the relevant parts):这是产品和经销商模型(仅相关部分):
class Product(models.Model, CloneMixin):
published = models.BooleanField(null=True,blank=True, default=False)
title = models.CharField(null=True,blank=True,max_length=100)
subtitle = models.CharField(null=True,blank=True,max_length=200)
(...)
dealer = models.ManyToManyField(Dealer, null=True,editable=True,related_name="product_to_dealer")
(...)
# Not sure what this does:
_clone_many_to_many_fields = ['parameter', 'tag', 'category', 'dealer','images']
(...)
class Dealer(models.Model, CloneMixin):
dealer_check = models.ForeignKey("self",null=True, blank=True, on_delete=models.SET_NULL)
published = models.BooleanField(null=True,blank=True, default=True)
(...)
users = models.ManyToManyField(User,null=True,blank=True, related_name='dealers')
(...)
As it is, my ListView just displays every products there is in the db.事实上,我的 ListView 只显示数据库中的所有产品。 From what I've understood, I need something like
据我了解,我需要类似的东西
def get_queryset(self):
return Product.objects.filter(user=self.request.user)
or in the get_context_data, it would do the same I guess:或者在 get_context_data 中,我猜它会做同样的事情:
context["product_list"] = context["product_list”].filter(user=self.request.user)
But in this case, the user related to the product is in the Dealer model (ManyToManyField in the Product model).但在这种情况下,与产品相关的用户在经销商 model(产品模型中的ManyToManyField)中。 I want to ask django "Show me only the products for which the current logged-in user is in the "users" of the dealer related to this product."
我想问 django “只显示当前登录用户在与该产品相关的经销商的“用户”中的产品。”
I don't think that's too complicated by I need some pointers on this.我不认为这太复杂了,因为我需要一些指示。 Thanks for your help.
谢谢你的帮助。
Something like?就像是?
Product.objects.filter(dealer__users = request.user).distinct()
You may / may not need the distinct method depending on the relations of your data.根据您的数据关系,您可能/可能不需要不同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.