Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String 'None' asigned to Field for empty elements #7

Open
jgomo3 opened this issue Feb 13, 2017 · 2 comments
Open

String 'None' asigned to Field for empty elements #7

jgomo3 opened this issue Feb 13, 2017 · 2 comments

Comments

@jgomo3
Copy link

jgomo3 commented Feb 13, 2017

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.

@fdintino
Copy link
Member

It wasn't intentional. I had mistakenly assumed that _Element.text mapped more-or-less to DOM level 3 DOMNode.textContent, which has an empty string for an element without content. Which do you think makes more sense here, '' or None? I'd be inclined to go with an empty string.

@jgomo3
Copy link
Author

jgomo3 commented Feb 13, 2017

I would say an empty string due is what is commonly use in Django projects:

In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.Field.null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants