1 2 3 4 5 6 7 8 9 10 11 12 | from django.db.models import signals
from django.dispatch import dispatcher
from django.db import models
def kill_gremlins(text):
return unicode(text).encode('iso-8859-1').decode('cp1252')
def charstrip(sender, instance):
for i_attr in instance._meta.fields:
if type(i_attr) == models.TextField or type(i_attr) == models.CharField:
if getattr(instance, i_attr.name):
setattr(instance, i_attr.name, kill_gremlins(getattr(instance, i_attr.name)))
dispatcher.connect(charstrip, signal=signals.pre_save)
|
Comments
very nice/clean approach with the signals,
but the kill_gremlins function seems to be a little over-complex to me.
i mean, cannot we achieve the same with:
?
(assuming that we are dealing with mishandled unicode-strings.
#
Yes, that does appear to work correctly. I thought that route would drop the non iso compatible characters, but it appears to be correctly making the conversion. Very nice, I will update the method.
#
Note that encode("iso-8859-1") does not handle non-latin-1 characters in a Unicode string (obviously):
Maybe the usecase for this snippet is more limited, but it's not a full replacement for my (rather dated) code.
#