__unicode__ and return.self

18 views
Skip to first unread message

spike

unread,
Mar 25, 2012, 12:10:52 AM3/25/12
to django...@googlegroups.com
Running the tut here:  https://docs.djangoproject.com/en/1.1/intro/tutorial01/  but I'm having trouble understanding what the point of __unicode__ is. Also, what is the point of the return statements? I've read the docs over and over but don't get it.

Jonathan Baker

unread,
Mar 25, 2012, 12:19:51 AM3/25/12
to django...@googlegroups.com
The __unicode__ method allows models to return a more usable representation of a record instead of a unicode object. This is useful in a variety of places, from the Django admin to using the manage.py shell on the command line.

The return statement effectively ends the execution of a particular function or method by returning something (an object, string, whatever you may need to return for the task at hand).

On Sat, Mar 24, 2012 at 10:10 PM, spike <pwashi...@gmail.com> wrote:
Running the tut here:  https://docs.djangoproject.com/en/1.1/intro/tutorial01/  but I'm having trouble understanding what the point of __unicode__ is. Also, what is the point of the return statements? I've read the docs over and over but don't get it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jKgyKR2iRdgJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Jonathan D. Baker
Web Developer
http://jonathandbaker.com
303.257.4144
Message has been deleted

MF-DOS

unread,
Mar 25, 2012, 12:34:02 AM3/25/12
to Django users
Thanks for the reply.

If my model has 5 fields, why am I only returning one or two?
Shouldn't I return all 5?

On Mar 24, 9:19 pm, Jonathan Baker <jonathandavidba...@gmail.com>
wrote:

Jonathan Baker

unread,
Mar 25, 2012, 12:48:31 AM3/25/12
to django...@googlegroups.com
Glad to help. How the record is represented is up to you. For instance, if I have a Person model with the fields first_name, middle_name, last_name, email_address and phone number, I would think it prudent to define __unicode__ as:

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

... since first + last name is a common representation for a person. It would be just as valid (and perhaps even more usable in some situations) to return a middle name as well.

Masklinn

unread,
Mar 25, 2012, 5:00:26 AM3/25/12
to django...@googlegroups.com
On 2012-03-25, at 06:27 , spike wrote:
> Thanks for the reply.
>
> Why am I only returning one or two fields from each model? If I define 5
> fields, shouldn't I return all 5?

There is no "should" when it comes to __unicode__, you can return
whatever you want. But the *purpose* of unicode is to return a
human-readable default identification/representation of the object, not
to encode the object's state.

For instance let's say you're creating a mass transit site with bus
lines. A bus line may have dozens of attributes with various information
such as the line's endpoints or the line's supervisor or whatever. But
you don't want to display those every time you refer to the bus line.
Instead, you probably just want "Line <number of the line>". That's what
you set up in __unicode__, a single field:

def __unicode__(self):
return u"Line %s" % self.line_number

Side-note: neither `return` nor `__unicode__` are features of Django,
you may want to read through the Python tutorial at least.

Jonathan Baker

unread,
Mar 25, 2012, 1:50:10 AM3/25/12
to django...@googlegroups.com
You're only returning two fields from the __unicode__ method, not the model as a whole. All of the fields are available when importing the model. The __unicode__ method simply affects how a record from a model is displayed within particular areas such as the admin (but not regular template output).

On Sat, Mar 24, 2012 at 10:27 PM, spike <pwashi...@gmail.com> wrote:
Thanks for the reply.

Why am I only returning one or two fields from each model? If I define 5 fields, shouldn't I return all 5?

On Saturday, March 24, 2012 9:19:51 PM UTC-7, jondbaker wrote:
The __unicode__ method allows models to return a more usable representation of a record instead of a unicode object. This is useful in a variety of places, from the Django admin to using the manage.py shell on the command line.

The return statement effectively ends the execution of a particular function or method by returning something (an object, string, whatever you may need to return for the task at hand).

On Sat, Mar 24, 2012 at 10:10 PM, spike <pwashi...@gmail.com> wrote:
Running the tut here:  https://docs.djangoproject.​com/en/1.1/intro/tutorial01/  but I'm having trouble understanding what the point of __unicode__ is. Also, what is the point of the return statements? I've read the docs over and over but don't get it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/​msg/django-users/-/​jKgyKR2iRdgJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@​googlegroups.com.

For more options, visit this group at http://groups.google.com/​group/django-users?hl=en.



--
Jonathan D. Baker
Web Developer
http://jonathandbaker.com
303.257.4144

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/IFpUg5EEj7oJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply all
Reply to author
Forward
0 new messages