我想要做的只是通过标签将图像与部门模型匹配。如何为我的部门页面编写视图来执行此操作?我到处都看,但找不到答案。请帮忙!
型号:
class Tag(models.Model):
tag_title = models.CharField(max_length=200)
slug = models.CharField(max_length=200)
def __str__(self):
return self.tag_title
class Image(models.Model):
image_url = models.CharField(max_length=200)
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
def __str__(self):
return self.title
class Department(models.Model):
page_title = models.CharField(max_length=200)
slug = models.CharField(max_length=200)
content = models.TextField()
tags = models.ManyToManyField(Tag, blank=True)
def __str__(self):
return self.page_title
答案 0 :(得分:1)
试试这个:
Image.objects.filter(tags__department=d) #where d is your deparment.
答案 1 :(得分:0)
这样的事情应该有效:
给定Department
对象department_obj
:
images = Image.objects.filter(tags__in=department_obj.tags.all())