-
Notifications
You must be signed in to change notification settings - Fork 14
Description
This line is causing an XPathTextField to receive the value 'None' (where type('None') == str) for empty XML elements.
This is happening because force_unicode (or force_text in my case as I'm using Python3) when not given the strings_only key attribute a value of True, will return the string 'None' for empty elements.
>>> from django.utils.encoding import force_text
>>> from lxml import etree
>>>
>>> xml = '<A />'
>>> tree = etree.fromstring(xml)
>>> text = tree.xpath('/A')[0].text
>>> type(text)
<class 'NoneType'>
>>> force_text(text)
'None'
>>> type(force_text(text)
<class 'str'>It is difficult to me to determine if this is actually a bad thing. But I'll share with you my experience:
I'm using django-xml as a interface for interacting with all my XMLs. Sometimes, I have to store some information from those XML in a database. In that context, it is bad to have a string 'None' instead of the actual None value or an empty string ''.
Maybe the Django force_text behaviour has a good reason behind: I suppose that as it was designed with the presentation layer in mind and not the model layer.