string = 'After\u20e1 trunc, this should be 20 characters...'
temp = Truncator(string).chars(20)
print(len(temp))
this outputs:
21
If we were to remove the unicode symbol, it would correctly output 20. Whether this is truly intended or not eludes me.
When trying to put a string truncated in such manner into a CharField, it fails to pass the MaxLengthValidator check, as it internally uses len() for its check.
Since Python 3, strings respect Unicode by default, so slices seem to work fine for truncation.
Django's
Truncator(text).chars(n)
seems to ignore all unicode combining characters, resulting in incorrect string truncations.For example,
this outputs:
If we were to remove the unicode symbol, it would correctly output 20. Whether this is truly intended or not eludes me.
When trying to put a string truncated in such manner into a CharField, it fails to pass the MaxLengthValidator check, as it internally uses len() for its check.
Since Python 3, strings respect Unicode by default, so slices seem to work fine for truncation.