可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Apparently PostgreSQL 8.4 and Ubuntu 10.04 cannot handle the updated way to sort W and V for Swedish alphabet. That is, it's still ordering them as the same letter like this (old definition for Swedish ordering):
it should be (new definition for Swedish ordering):
The examples gives:
Ordering One to respect locale. By then using the second item (object id) in each tuple I could fetch the correct object in Django.
I'm starting to doubt that this will be possible? Would upgrading PostgreSQL to a newer version that handles collations better and then use raw SQL in Django be possible?
回答1:
When running LC_ALL=sv_SE.UTF-8 sort
on your example on Ubuntu-10.04, it comes out with Wa before Vb (the "old way"), so Ubuntu does not seem to agree with the "new way". Since PostgreSQL relies on the operating system for this, it will behave just the same as the OS given the same lc_collate.
There is actually a patch in debian glibc related to this particular sort issue: http://sourceware.org/bugzilla/show_bug.cgi?id=9724 But it was objected to and not accepted. If you only need this behavior on a system you administer, you can still apply the change of the patch to /usr/share/i18n/locales/sv_SE and rebuild the se_SV locale by running locale-gen sv_SE.UTF-8
. Or better yet, create your own alternative locale derived from it to avoid messing with the original.
回答2:
This solution is complex because key=locale.strxfrm works fine with single lists and dictionaries, but not with lists of lists or lists of tuples.
Changes from Py2 -> Py3 : use locale.setlocale(locale.LC_ALL, '') and key='locale.strxfrm' (instead of 'cmp=locale.strcoll').