I'm trying to compare two strings, the first one, s1, comes from mongoengine and the second one, s2, comes from a Django http request.
They look like this:
>>> s1 = product_model.Product.objects.get(pk=1).name
>>> s1
u'Product \xe4 asdf'
>>> s2 = request.POST['name']
>>> s2
'Product \xc3\xa4 asdf'
They have the same letter in them, the Swedish 'ä', but mongoengines (s1) is in a Python unicode string and Djangos (s2) is in a Python bytestring with unicode encoded characters.
I can easily solve this by e.g. converting the Python unicode string to be a byte string
>>> s1.encode('utf-8') == s2
True
But I would like to think that the best-practice is to have all my Python strings encoded the same way in my system, correct?
How can I tell Django to use Python unicode strings instead? Or how can I tell MongoEngine to use unicode encoded Python bytestrings?
request.POST['name']should always give you a Unicode string. Django automatically decodes POST values to Unicode before it ever gets to your view.