Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
django docs __unicode__ return u'%s %s
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Krondaj  
View profile  
 More options Jan 24 2012, 6:04 pm
From: Krondaj <c.d.smi...@gmail.com>
Date: Tue, 24 Jan 2012 15:04:32 -0800 (PST)
Local: Tues, Jan 24 2012 6:04 pm
Subject: django docs __unicode__ return u'%s %s
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Krondaj  
View profile  
 More options Jan 24 2012, 6:06 pm
From: Krondaj <c.d.smi...@gmail.com>
Date: Tue, 24 Jan 2012 15:06:56 -0800 (PST)
Local: Tues, Jan 24 2012 6:06 pm
Subject: Re: django docs __unicode__ return u'%s %s
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)

On Jan 24, 11:04 pm, Krondaj <c.d.smi...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alec Koumjian  
View profile  
 More options Jan 24 2012, 6:10 pm
From: Alec Koumjian <akoumj...@gmail.com>
Date: Tue, 24 Jan 2012 15:10:15 -0800 (PST)
Local: Tues, Jan 24 2012 6:10 pm
Subject: Re: django docs __unicode__ return u'%s %s

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

Full documentation is
here: http://docs.python.org/library/stdtypes.html#string-formatting-operat...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Bennett  
View profile  
 More options Jan 24 2012, 7:33 pm
From: James Bennett <ubernost...@gmail.com>
Date: Tue, 24 Jan 2012 18:33:40 -0600
Local: Tues, Jan 24 2012 7:33 pm
Subject: Re: django docs __unicode__ return u'%s %s

On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.smi...@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."


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kenneth gonsalves  
View profile  
 More options Jan 25 2012, 1:33 am
From: kenneth gonsalves <law...@thenilgiris.com>
Date: Wed, 25 Jan 2012 12:03:51 +0530
Local: Wed, Jan 25 2012 1:33 am
Subject: Re: django docs __unicode__ return u'%s %s

On Tue, 2012-01-24 at 18:33 -0600, James Bennett wrote:
> On Tue, Jan 24, 2012 at 5:04 PM, Krondaj <c.d.smi...@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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
coded kid  
View profile  
 More options Jan 25 2012, 1:40 am
From: coded kid <duffleboi...@gmail.com>
Date: Tue, 24 Jan 2012 22:40:39 -0800 (PST)
Local: Wed, Jan 25 2012 1:40 am
Subject: Re: django docs __unicode__ return u'%s %s
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Petr Přikryl  
View profile  
 More options Jan 25 2012, 6:11 am
From: "Petr Přikryl" <prik...@atlas.cz>
Date: Wed, 25 Jan 2012 12:11:48 +0100
Local: Wed, Jan 25 2012 6:11 am
Subject: Re: django docs __unicode__ return u'%s %s

> "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-operat...
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Krondaj  
View profile  
 More options Jan 25 2012, 10:07 am
From: Krondaj <c.d.smi...@gmail.com>
Date: Wed, 25 Jan 2012 07:07:17 -0800 (PST)
Local: Wed, Jan 25 2012 10:07 am
Subject: Re: django docs __unicode__ return u'%s %s
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Petr Přikryl  
View profile  
 More options Jan 25 2012, 5:09 pm
From: "Petr Přikryl" <prik...@atlas.cz>
Date: Wed, 25 Jan 2012 23:09:04 +0100
Local: Wed, Jan 25 2012 5:09 pm
Subject: Re: django docs __unicode__ return u'%s %s

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
JohnA  
View profile  
 More options Jan 25 2012, 6:00 pm
From: JohnA <john.armstrong....@gmail.com>
Date: Wed, 25 Jan 2012 15:00:26 -0800 (PST)
Local: Wed, Jan 25 2012 6:00 pm
Subject: Re: django docs __unicode__ return u'%s %s
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.

On Jan 24, 6:04 pm, Krondaj <c.d.smi...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
coded kid  
View profile  
 More options Jan 27 2012, 3:31 am
From: coded kid <duffleboi...@gmail.com>
Date: Fri, 27 Jan 2012 00:31:34 -0800 (PST)
Local: Fri, Jan 27 2012 3:31 am
Subject: Re: django docs __unicode__ return u'%s %s
Hey Petr, thats what I'm trying to say. Just that I didnt construct my
sentences well. Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »