简体   繁体   English

Django 模板:如果..在

[英]Django templates: if .. in

I have a list of products and want to display them in a template, depending on whether or not the product's dealer is "owned" by the current logged user or not.我有一个产品列表并希望在模板中显示它们,具体取决于产品的经销商是否由当前登录的用户“拥有”。 So here's my view:所以这是我的观点:

class ArticleListView(ListView, LoginRequiredMixin):
    template_name = "accounts/list_article_quentin.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)
        # Get a list of all dealers associated to the current users.
        currentUserDealers = self.request.user.dealers.all().values_list('id', flat=True)
        context["current_user_dealers"] = list(currentUserDealers)
        return context

so currentUserDealers is the list of the dealers associated to the current user (It's a ManyToMany relationship between dealers and users).所以 currentUserDealers 是与当前用户关联的经销商列表(经销商和用户之间的多对多关系)。

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')
    (...) 

In my template, I would like to do something like that to decide wether to display the product or not:在我的模板中,我想做类似的事情来决定是否展示产品:

{% if product.dealer.all.0.pk in current_user_dealer %}

(there can be multiple dealers for a product but I'll see that later, hence the all.0) (一个产品可以有多个经销商,但我稍后会看到,因此是 all.0)

Obviously this doesn't work but you get the idea, I want to see if the current product's dealer ID is in the list of current user's associated dealers, to see if we display it or not.显然这不起作用,但你明白了,我想看看当前产品的经销商 ID 是否在当前用户的关联经销商列表中,看看我们是否显示它。 What's the syntax to do it in the template?在模板中执行此操作的语法是什么? Or maybe there's a better way to do this directly in the view?或者也许有更好的方法可以直接在视图中执行此操作?

This query should get you the correct product_list in your template此查询应该在您的模板中为您提供正确的product_list

def get_context_data(self):
    context = super(ArticleListView, self).get_context_data(**kwargs)
    context['product_list'] = Product.objects.filter(dealer__id_in=self.request.user.dealers.all())
    return context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM