Changes between Version 5 and Version 6 of UnicodeBranch

05/12/2007 02:22:16 AM (11 years ago)
Malcolm Tredinnick

Added information about what a unicode-aware app needs to consider.


  • UnicodeBranch

    v5 v6  
    4949Finally, some documentation needs to be written describing good practices for creating unicode-aware Django apps.
     51== Porting Applications ==
     53One of the design goals of the Unicode branch is that very litte significant changes to existing third-party code should be required. However, there are some thigns that developers should be aware of when writing applications designed to handle international input.
     55'''** This section is no doubt incomplete. User experiences are welcome. If you discover something that is necessary to change, please add a bullet-point to the list (although we may edit the list periodically to be more coherent). **'''
     57=== String Encoding ===
     59 * In many cases, Django will convert any bytestrings passed to functions, such as filter functions, into unicode strings. All bytestrings, with the exception of form inputs and data read from files, are assumed to be UTF-8 encoded. Internal bytestrings that are not valid UTF-8 will cause fatal exceptions (because {{{my_string.decode('utf-8')}}} will fail).
     61 * Template files read from disk may be in an encoding that is not related to the output encoding or UTF-8. To specify the on-disk file encoding, use the `FILE_CHARSET` setting, which is new in the Unicode branch.
     63 * String data read from the database will be converted directly to unicode strings. So model attributes based on text fields (!TextField, !CharField, etc) will be unicode strings.
     65 * Field sizes for text fields such as !TextField and !CharField are specified in terms of characters, not the number of bytes used in the encoding in the database. All databases supported by Django can handle this (i.e. their ''VARCHAR'' fields are sized in terms of characaters and can store unicode characters). So you do '''not''' need to worry about how many bytes the encoded version of your data will take up when working with lengths.
     67=== Databases ===
     69 * Make sure that your database tables support an encoding that can hold all the data you are going to send to it. For example, if you may possibly be sending Chinese characters to the database, using the Russian KOI8-R encoding is going to cause errors. Django does not need to know what encoding your database uses, since the Python database wrappers take care of that. However, you should ensure your database is configured to handle the data you wish to send it. Generally, using a UTF-8 encoding for your tables is the simplest solution.
     70    * '''TODO''': Write up how to set and check this information for MySQL, PostgreSQL and SQLite.
     72=== Models ===
     74 * As mentioned previously, all model attributes retrieved from the database will be unicode strings.
     76 * If you are supporting international data, it is not safe to return the value of a field directly in your model's {{{__str__}}} method (in Python, {{{__str___}}} will always coerce the result to a bytestring object, even if you return a unicode string from the function). There are two possibilities here:
     77    * The simplest solution is to replace any {{{__str__}}} methods with a {{{__unicode__}}} method. This method returns a unicode string, so you can safely write
     80class MyModel(models.Model):
     81    name = models.CharField(maxlength=50)
     82    ...
     83    def __unicode__(self):
     84        return
     86    The default {{{models.Model.__str__}}} method will call your model's {{{__unicode__}}}, if it exists, and then convert the result to UTF-8. So this single change should be transparent to the rest of your code.
     87    * Alternatively, if you want to explicitly write the {{{__str__}}} method for your model, it '''must''' return a UTF-8 encoded bytestring. No other encoding is acceptable here (certainly '''not''' {{{settings.DEFAULT_CHARSET}}}), because the result of calling {{{str()}}} on a model is used in more places than just template output.
Back to Top