开发者

How do get the different values that takes an attribute with a django query?

开发者 https://www.devze.com 2022-12-20 19:07 出处:网络
I have this Model in django : class Post(models.Model): title = models.CharField(max_length=255) category = mode开发者_JS百科ls.CharField(max_length=255)

I have this Model in django :

class Post(models.Model):
    title = models.CharField(max_length=255)
    category = mode开发者_JS百科ls.CharField(max_length=255)

I would like to get the different values that are used in the category attribute.

For example, if we consider this db :

Post(title = "title 1", category="foo")
Post(title = "title 2", category="bar")
Post(title = "title 3", category="foo")

the query should give me ("foo", "bar")


Use distinct() and values_list():

Post.objects.values_list('category', flat=True).distinct();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号