我们实现了LowerCaseCharField.我们很乐意听到更好的实施建议.
from django.db.models.fields import CharField
class LowerCaseCharField(CharField):
"""
Defines a charfield which automatically converts all inputs to
lowercase and saves.
"""
def pre_save(self, model_instance, add):
"""
Converts the string to lowercase before saving.
"""
current_value = getattr(model_instance, self.attname)
setattr(model_instance, self.attname, current_value.lower())
return getattr(model_instance, self.attname)
Run Code Online (Sandbox Code Playgroud)
事实上我们喜欢的是:
> modelinstance.field_name="TEST"
> print modelinstance.field_name
'test'
Run Code Online (Sandbox Code Playgroud)
当前实现仅在保存时转换为小写.
我使用谷歌应用程序引擎,不能使用任何C/C++扩展,只有纯和pythonic库来转换Unicode/UTF-8字符串到大/小写.str.lower()和string.lowercase()没有.