Search code examples
python-3.xpyyamldjango-2.2

How do I consume files in Django 2.2 for yaml parsing?


I'm trying to upgrade my site from Django 1.11 to Django 2.2, and I'm having trouble with uploading and parsing yaml files.

The error message is:

ScannerError : mapping values are not allowed here in "", line 1, column 34: b'---\n recipeVersion: 9\n name: value\n' ^

I'm getting the file contents using a ModelForm with a widget defined as:

'source': AsTextFileInput()

... using ...

class AsTextFileInput(forms.widgets.FileInput):
    def value_from_datadict(self, data, files, name):
        return files.get(name).read()

... and then I get the source variable to parse with:

cleaned_data = super(RecipeForm, self).clean()
source = cleaned_data.get("source")

From that error message above, it looks like my newlines are being escaped, so yaml sees the text all on a single line. I tried logging the source of this file, and here's how it shows in my log file:

DEBUG b'---\n recipeVersion: 9\n name: value\n'

So, how can I get this file content without (what looks to me like) escaped newlines so I can parse it as yaml?

Edit: my code and yaml (simplified for this question) have not changed; upgrading Python projects has broken the parsing.


Solution

  • Decoding the bytestring fixed it:

    class AsTextFileInput(forms.widgets.FileInput):
        def value_from_datadict(self, data, files, name):
            return files.get(name).read().**decode('utf-8')**