我有一个标签模型,每个标签都有一个类别:
class Tag(models.Model):
name = models.CharField('Name', max_length=35)
description = models.CharField('Description', max_length=255)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
我还有一个用户&标签关系模型:
class UserTag(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='User', related_name='user_tag')
tag = models.ForeignKey(Tag, verbose_name='Tag', related_name='user_tag')
preferred = models.BooleanField(verbose_name='Preferred', default=False)
created_at = models.DateTimeField('Created at', auto_now_add=True)
updated_at = models.DateTimeField('Updated at', auto_now=True)
如何查询特定用户的所有不同类别名称?
答案 0 :(得分:0)
假设分类模型中的名称列
list(set(UserTag.objects.filter(user=request.user).values_list('tag__category__name',flat=True))