Re: model string reprentations

2008-12-12 Thread Malcolm Tredinnick


On Fri, 2008-12-12 at 09:50 -0500, Karen Tracey wrote:
> On Fri, Dec 12, 2008 at 7:33 AM, ben852  wrote:
> 
> Hi,
> I am new to django and programming.
> I have a problem with the method _str_( ).
> Following the tutorial, I edited my models.py file in
> mysite/books and
> wrote:
> [snip]
> 
> The fact that you are using __str__ instead of __unicode__ indicates
> you are using a very old release of Django.  If you are just starting
> out you should really start with the latest 1.0.2 release, it is a
> much better base to learn and build on, with many significant feature
> additions, countless bugs fixed, and API stability.

I hate to write this, since it will no doubt complicate the situation,
but ...

Whilst using __unicode__ is preferable in some respects (will certainly
lead to neater code), Django also handles using __str__ in models. The
developer is responsible for ensuring that __str__ returns UTF-8 encoded
"str" objects, but it's actually the __str__ method that is called in a
lot of cases. Internally, the default Model.__str__ checks for a
__unicode__ method and calls that, encoding the output as UTF-8. So
using __str__ isn't illegal or anything -- but the original poster's
code will fail as written the first time somebody uses non-ASCII
characters in their name, since it's not encoding the output.

Regards,
Malcolm

> 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: model string reprentations

2008-12-12 Thread Karen Tracey
On Fri, Dec 12, 2008 at 7:33 AM, ben852  wrote:

>
> Hi,
> I am new to django and programming.
> I have a problem with the method _str_( ).
> Following the tutorial, I edited my models.py file in mysite/books and
> wrote:
> [snip]


The fact that you are using __str__ instead of __unicode__ indicates you are
using a very old release of Django.  If you are just starting out you should
really start with the latest 1.0.2 release, it is a much better base to
learn and build on, with many significant feature additions, countless bugs
fixed, and API stability.

Karen

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: model string reprentations

2008-12-12 Thread Tim Chase

> Sure do:  it should be __str__() with two underscores on either side,
> not one.

And for the use-case the OP gave...

>> from books.models import Publisher
>> publisher_list = Publisher.objects.all ( )
>> publisher_list
>>
>> [, ]  #


Python calls the __repr__ for the display at the command-line, 
not the __str__ (the str is used for prints:

   >>> print publisher_list # use the __str__ method
   [output]
   >>> publisher_list # uses the __repr__ method
   [output]

which is the same as

   >>> print repr(publisher_list)

-tim




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: model string reprentations

2008-12-12 Thread ben852

thanks a lot

On Dec 12, 3:06 pm, Jeff FW  wrote:
> Sure do:  it should be __str__() with two underscores on either side,
> not one.
>
> -Jeff
>
> On Dec 12, 7:33 am, ben852  wrote:
>
> > Hi,
> > I am new to django and programming.
> > I have a problem with the method _str_( ).
> > Following the tutorial, I edited my models.py file in mysite/books and
> > wrote:
>
> > class Publisher(models.Model):
> >   name = models.Charfield(max_length=30)
> >   address = models.Charfield(max_length=30)
> >   website = models.URLField()
>
> >   def _str_(self):
> >return self.name
>
> > then python manage.py shell
>
> > from books.models import Publisher
> > publisher_list = Publisher.objects.all ( )
> > publisher_list
>
> > [, ]  #
> > p1.save ( ) and p2.save ( )
>
> > The _str_ method doesn't work.
> > Do you have an idea?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: model string reprentations

2008-12-12 Thread Jeff FW

Sure do:  it should be __str__() with two underscores on either side,
not one.

-Jeff

On Dec 12, 7:33 am, ben852  wrote:
> Hi,
> I am new to django and programming.
> I have a problem with the method _str_( ).
> Following the tutorial, I edited my models.py file in mysite/books and
> wrote:
>
> class Publisher(models.Model):
>       name = models.Charfield(max_length=30)
>       address = models.Charfield(max_length=30)
>       website = models.URLField()
>
>                   def _str_(self):
>                            return self.name
>
> then python manage.py shell
>
> from books.models import Publisher
> publisher_list = Publisher.objects.all ( )
> publisher_list
>
> [, ]  #
> p1.save ( ) and p2.save ( )
>
> The _str_ method doesn't work.
> Do you have an idea?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



model string reprentations

2008-12-12 Thread ben852

Hi,
I am new to django and programming.
I have a problem with the method _str_( ).
Following the tutorial, I edited my models.py file in mysite/books and
wrote:

class Publisher(models.Model):
  name = models.Charfield(max_length=30)
  address = models.Charfield(max_length=30)
  website = models.URLField()

  def _str_(self):
   return self.name

then python manage.py shell

from books.models import Publisher
publisher_list = Publisher.objects.all ( )
publisher_list

[, ]  #
p1.save ( ) and p2.save ( )

The _str_ method doesn't work.
Do you have an idea?



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---