0

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.

asked Dec 17, 2014 at 3:48

1 Answer 1

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 \u escape sequence, which is followed by four hex digits giving the code point. The \U escape 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
answered Dec 17, 2014 at 3:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.