Skip to content

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

@jgomo3

Description

@jgomo3

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions