Django serialize function returns error JSONEncoder.__init__() got an unexpected keyword argument 'fields'

Question:

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:UsersVahid MoradiAppDataLocalProgramsPythonPython310libthreading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:UsersVahid MoradiAppDataLocalProgramsPythonPython310libthreading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoremanagementcommandsrunserver.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:ProjectsNavid MotorWebsiteDjangoNavidMotor.comnavidmotorurls.py", line 5, in <module>
    path("", include("core.urls")),
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangourlsconf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "C:UsersVahid MoradiAppDataLocalProgramsPythonPython310libimportlib__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:ProjectsNavid MotorWebsiteDjangoNavidMotor.comcoreurls.py", line 2, in <module>
    from .views import (
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.comcoreviews.py", line 13, in <module>
    class HomePageView(ListView):
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.comcoreviews.py", line 31, in HomePageView
    context = {"markers": json.loads(serialize("geojson",
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjangocoreserializers__init__.py", line 134, in serialize
    s.serialize(queryset, **options)
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjgeojsonserializers.py", line 471, in serialize
    self.end_serialization()
  File "D:ProjectsNavid MotorWebsiteDjangoNavidMotor.com.venvlibsite-packagesdjgeojsonserializers.py", line 253, in end_serialization
    json.dump(self.feature_collection, self.stream, cls=cls, **self.options)
  File "C:UsersVahid MoradiAppDataLocalProgramsPythonPython310libjson__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"}
Asked By: Vahid

||

Answers:

From your error traceback it can be noticed that some code in djgeojsonserializers.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"}
Answered By: Abdul Aziz Barkat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.