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