Changes between Version 146 and Version 147 of RemovingTheMagic


Ignore:
Timestamp:
05/10/2006 02:34:25 PM (9 years ago)
Author:
Tom Tobin <korpios@…>
Comment:

You can code-escape with backticks. :-) (Doesn't help with the TOC, though.)

Legend:

Unmodified
Added
Removed
Modified
  • RemovingTheMagic

    v146 v147  
    452452}}}
    453453
    454 === __repr__() model method is replaced by __str__() ===
    455 
    456 Or seems to be afaics. Hm, how do I get those underscores to display as underscores instead of underline formatting?
     454=== `__repr__()` model method is replaced by `__str__()` ===
     455
     456You should use `__str__()` where `__repr__()` was formerly used, i.e., as a string representation of the model.
     457
     458`__repr__()` returns to its standard Python purpose of returning a string suitable for debugging purposes (e.g., `<ObjectName more_info>`); unless you want to customize the returned string, explicitly overriding `__repr__` in a model class is unnecessary.
     459
     460For example::
     461
     462{{{
     463class BlogPost(models.Model):
     464
     465    [...]
     466
     467    def __repr__(self):
     468        return '''<BlogPost title="%s" comments_enabled=%s>''' % (self.title, self.comments_enabled)
     469
     470    def __str__(self):
     471        return self.title
     472}}}
     473
    457474
    458475=== Database connection relocated/renamed ===
Back to Top