Home > Software design >  Django serialize function returns error JSONEncoder.__init__() got an unexpected keyword argument &#
Django serialize function returns error JSONEncoder.__init__() got an unexpected keyword argument &#

Time:11-07

I'm trying to serilize my salesnetwork.agency model into a geojson file, and according to Django docs I'm using the following code:

(I'm using Django 4.1.2)

markers = Agency.objects.all()
geojson_file = serialize("geojson",
                          markers,
                          geometry_field="location",
                          fields=("province",)
               )

But this code results in the following error:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
...
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\navidmotor\urls.py", line 5, in <module>
    path("", include("core.urls")),
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\urls\conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\core\urls.py", line 2, in <module>
    from .views import (
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\core\views.py", line 13, in <module>
    class HomePageView(ListView):
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\core\views.py", line 31, in HomePageView
    context = {"markers": json.loads(serialize("geojson",
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\serializers\__init__.py", line 134, in serialize
    s.serialize(queryset, **options)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\djgeojson\serializers.py", line 471, in serialize
    self.end_serialization()
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\djgeojson\serializers.py", line 253, in end_serialization
    json.dump(self.feature_collection, self.stream, cls=cls, **self.options)
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 173, in dump
    iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
TypeError: JSONEncoder.__init__() got an unexpected keyword argument 'fields'

Which is about the field field in the serialize function. So where is my problem? I wrote as the docs instructed.

I've also added the following to my settings:

SERIALIZATION_MODULES = {"geojson": "djgeojson.serializers"}

CodePudding user response:

From your error traceback it can be noticed that some code in djgeojson\serializers.py is called instead of django/contrib/gis/... also as you've confirmed you've set the following in your setting:

SERIALIZATION_MODULES = {"geojson": "djgeojson.serializers"}

This means when you want Django to use a formatter with the name being "geojson" it's going to use it from djgeojson.serializers, you can either remove that setting if you don't use the serializer from that package or if you want to continue using it you could simply update the name you use for it:

SERIALIZATION_MODULES = {"djgeojson": "djgeojson.serializers"}
  • Related