django docs __unicode__ return u'%s %s

614 views
Skip to first unread message

Krondaj

unread,
Jan 24, 2012, 6:04:32 PM1/24/12
to Django users
Hi,

in the django docs about __unicode__ it says the following:

class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)

def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)

what does the u'%s %s' % mean... I cannot find any exaplanation of
this in the docs?

i've seen this in someones code that was kindly lent to me by one of
the RC chat room people:

return u'ID%s: %s - %s - %s - %s' % (self.id, self.user,
self.question, self.answer, self.get_status_display())

but all this u' %s %s' %%%sss or what ever is most confusing....

is there a document or help guide some where that explains this
nomenclature, or method, system???

as I believe if you take the top example you should be able to do:
def __unicode__(self):
return(self.first_name, self.last_name)

If there is no documentation (for dummies) can anyone explain it to
me??

Thanks

Krondaj

Krondaj

unread,
Jan 24, 2012, 6:06:56 PM1/24/12
to Django users
sorry bottom line should be?

as I believe if you take the top example you should be able to do:
def __unicode__(self):
return (self.first_name, self.last_name)

Alec Koumjian

unread,
Jan 24, 2012, 6:10:15 PM1/24/12
to django...@googlegroups.com
The '%' symbol is used for formatting inside of a string. This is Python, not Django specific. It is a shortcut to insert variables (and format them to your liking) instead of doing messy stuff like this:

adjective = 'messy'
print 'This is the ' + adjective + ' way to do it'

As opposed to:
adjective = 'preferred'
print 'This is the %s way to do it' % adjective

James Bennett

unread,
Jan 24, 2012, 7:33:40 PM1/24/12
to django...@googlegroups.com
On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.s...@gmail.com> wrote:
> what does the u'%s %s' % mean... I cannot find any exaplanation of
> this in the docs?

It's not covered in Django's documentation because it's a standard
feature of the Python programming language -- this is basic Python
string formatting, which follows similar conventions to printf()-style
formatting in other languages.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Message has been deleted

kenneth gonsalves

unread,
Jan 25, 2012, 1:33:51 AM1/25/12
to django...@googlegroups.com
On Tue, 2012-01-24 at 18:33 -0600, James Bennett wrote:
> On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.s...@gmail.com> wrote:
> > what does the u'%s %s' % mean... I cannot find any exaplanation of
> > this in the docs?
>
> It's not covered in Django's documentation because it's a standard
> feature of the Python programming language -- this is basic Python
> string formatting, which follows similar conventions to printf()-style
> formatting in other languages.

soon to be deprecated in favour of using format.
--
regards
Kenneth Gonsalves

coded kid

unread,
Jan 25, 2012, 1:40:39 AM1/25/12
to Django users
If I may say, 'u' makes it easy to return variable with any hassle.
And %s rep each string in your script. I think there should be other
ways to go about this. Hope you get my point?

Petr Přikryl

unread,
Jan 25, 2012, 6:11:48 AM1/25/12
to django...@googlegroups.com

> "coded kid" wrote:
>If I may say, 'u' makes it easy to return variable with any hassle.
>And %s rep each string in your script. I think there should be other
>ways to go about this. Hope you get my point?

This is wrong. The prefix 'u' means that the following string is a Unicode
string, nothing more or less. It is a fact for Python 2.x. See
http://docs.python.org/reference/lexical_analysis.html#string-literals

The % character after the unicode string means "string interpolation",
i.e. replacing the placeholders inside the string template on the left.
See the String Formatting Operations http://docs.python.org/library/stdtypes.html#string-formatting-operations
You will also find the placeholder types there.

>> > Krondaj wrote:
>> > > what does the u'%s %s' % mean... I cannot find any exaplanation of
>> > > this in the docs?

The .format() method of the string type is the newer one, introduced
in Python 2.6. It is the preferred way now. But you can use it only
when you can be sure that the Python is 2.6 or newer. The command
in your __unicode__ method would look like:

def __unicode__(self):
return u'{0} {1}'.format(self.first_name, self.last_name)

The '{0}' is the placeholder for the self.first_name (zero'th argument,
the '{1}' is for the first argument (self.last_name), etc. Since Python 2.7
(if I recall correctly) you need not to use the numbers inside:

def __unicode__(self):
return u'{} {}'.format(self.first_name, self.last_name)


You do not want to return only...

def __unicode__(self):
return (self.first_name, self.last_name)

... because you want the method to return the unicode string representation.
Without .format() or without the % the method would return a tuple with
the information, not the string.

Petr

Krondaj

unread,
Jan 25, 2012, 10:07:17 AM1/25/12
to Django users
Hi Petr,

I'm using python 2.7.2

for another model i'm forming i have:

def __unicode__(self):
return (self.project_and_task, self.project_number,
self.date_created, self.requested_by)

to understand (i think the new method makes a little more sense?) the
format should be:

def __unicode__(self):
return u'{}  {} {} {}'.format(self.project_and_task,
self.project_number, self.date_created, self.requested_by)

is the two spaces between each {} two seperate the individual strings
with two spaces?

Krondaj





On Jan 25, 11:11 am, "Petr Přikryl" <prik...@atlas.cz> wrote:
> > "coded kid" wrote:
> >If I may say, 'u' makes it easy to return variable with any hassle.
> >And %s rep each string in your script. I think there should be other
> >ways to go about this. Hope you get my point?
>
> This is wrong.  The prefix 'u' means that the following string is a Unicode
> string, nothing more or less.  It is a fact for Python 2.x.  Seehttp://docs.python.org/reference/lexical_analysis.html#string-literals
>
> The % character after the unicode string means "string interpolation",
> i.e. replacing the placeholders inside the string template on the left.
> See the String Formatting Operationshttp://docs.python.org/library/stdtypes.html#string-formatting-operat...

Petr Přikryl

unread,
Jan 25, 2012, 5:09:04 PM1/25/12
to django...@googlegroups.com

"Krondaj wrote:
>I'm using python 2.7.2
>for another model i'm forming i have:
>
>def __unicode__(self):
> return (self.project_and_task, self.project_number,
> self.date_created, self.requested_by)
>
>to understand (i think the new method makes a little more sense?) the
>format should be:
>
>def __unicode__(self):
> return u'{}  {} {} {}'.format(self.project_and_task,
> self.project_number, self.date_created, self.requested_by)
>
>is the two spaces between each {} two seperate the individual strings
>with two spaces?

It is up to you. The purpose of the __unicode__() method
is to return nice and human readable Unicode string
that represents the object. It is used for example
in the admin mode that was designed to be general for
whatever model. Whenever the admin template needs to display
textual representation of the whole record as a string, it simply
calls the function for getting a string representation of the whole
object. The function calls the __unicode__() method that
is implemented by you. Whatever you want to see in the case
should be created by your definition of the method. Try to
put there strings that you can be sure they are yours -- just to
see what the method is called for.

Try

u'{}#{}***{}_{}'.format(self.project_and_task,
self.project_number, self.date_created, self.requested_by)

Also, you are not forced to display all the parts of the record.
It is only for the display purpose. It should be just a string
that represents the record. You probably want to put something
meaningful there.

Have a look at http://docs.python.org/library/functions.html#unicode
and http://docs.python.org/reference/datamodel.html#object.__unicode__

Petr

JohnA

unread,
Jan 25, 2012, 6:00:26 PM1/25/12
to Django users
Just to fill in some more info, __unicode__ methods are also part of
core python and just happen to appear a lot in django code because of
its preference for 16-bit unicode strings (though I think the methods
are actually mostly invoked in the admin). There is also a __str__
function for “ordinary” (8 bit potentially mbcs) strings. (This is
Python 2.x; Python 3.x is somewhat different.)

User-defined __unicode__ methods are overrides of builtin methods that
all objects are guaranteed to have. As the __...__ in the name
suggests, these methods are called implicitly in certain kinds of
expressions. Two cases I know of in Python 2.7 are expressions of
form unicode(obj) and expressions of form u’...%s...’ % obj (and I
assume other format expression tyopes where the format string is
unicode). In both, obj is converted to a unicode string using
whatever __unicode__ method is defined for the object.

coded kid

unread,
Jan 27, 2012, 3:31:34 AM1/27/12
to Django users
Hey Petr, thats what I'm trying to say. Just that I didnt construct my
sentences well. Thanks.
Reply all
Reply to author
Forward
0 new messages