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."
soon to be deprecated in favour of using format.
--
regards
Kenneth Gonsalves
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
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