外键过滤数据

yuvru6vn  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(224)

我想做一个按钮,过滤产品的类别
我添加了产品标签,因为我需要颜色、尺寸等。
如何从外键中的外键获取类别数据?在视图中.py
型号.py

  1. class Categorie(models.Model):
  2. naam = models.CharField(max_length=150,db_index=True)
  3. #
  4. #
  5. class Product(models.Model):
  6. slug = models.SlugField(unique=True, primary_key=True)
  7. titel = models.CharField(max_length=200)
  8. categorie = models.ForeignKey(Categorie, on_delete=models.CASCADE)
  9. #
  10. #
  11. class ProductAtribuut(models.Model):
  12. product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=False)
  13. price = models.FloatField(default=0)
  14. image = models.ImageField(blank=True, upload_to='gerechten/')
  15. visible = models.BooleanField(default=True)
  16. #
  17. #

我不能用这个来分类。
视图.py

  1. product = productatribuut.filter(categorie=categorie))
  2. return render(request, 'pages/index.html', {product})

如果有人知道,请给我一个例子,我可以怎么做。提前谢谢。真的很感激<三

cwtwac6a

cwtwac6a1#

你可以这样做:

  1. def function_name(request, category_id): # it could be category_id,or id, or slug if using slug depend on what you have and urls
  2. categorie= Categorie.objects.get(id=category_id)
  3. product = productatribuut.filter(categorie=categorie)
  4. return render(request, 'pages/index.html', {'product':product})

假设您将使用类别的id,

相关问题