1
class Tag(models.Model):
  parent = TreeForeignKey('self', null=True, blank=True, related_name='%(class)s_children')
  products = models.ManyToManyField('Product', through="ProductTags", blank=True)

class Product(models.Model):
  tags = models.ManyToManyField('Tag', through="ProductTags", blank=True)

tag_id in POST

currentTag = Tag.objects.get(id=tag_id)

childs = currentTag.get_children().filter(active=True)

应该选择具有当前标签的产品或当前标签的子标签

4

1 回答 1

1
from django.db.models import Q
products = Product.objects.filter(Q(tags = tag_id) | Q(tags__parent = tag_id))

应该可以解决问题。

于 2013-04-19T06:39:22.813 回答