I have a model which is as follow:
class Article (models.Model):
title = models.CharField(max_length=200)
body = models.CharField(max_length=200)
When I do
Article.objects.values_list('body',flat=True).distinct()
I get
[u'blah']
Ma
When I do Article.objects.values_list('title',flat=True).distinct()
I get
[u'test 1', u'test 2', u'test 3']
My question is, why is there a 'u' in front of all the output? Is there a way to get it without the u? Basically what I am trying to do is obtain the unique values of each column of my database. Maybe there is a better way to do this.
1 Answer 1
In Python, Unicode string literals are prepended with u or U:
In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character:
u'abcdefghijk'. Specific code points can be written using the\uescape sequence, which is followed by four hex digits giving the code point. The\Uescape sequence is similar, but expects 8 hex digits, not 4.
So what you're seeing simply indicates that your values are Unicode strings, not regular strings. If you use one of those values you generally won't see the u, e.g.
>>> print u'My String'
My String