# raise TypeError(f'Object of type {o.\_\_class\_\_.\_\_name\_\_} for serializers.serialize

**URL:** <https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914>\
**Category:** Mystery Errors\
**Created:** [February 17, 2023, 2:39pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914 "2023-02-17T14:39:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tenmia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tenmia/32/12251_2.png) [@Tenmia](https://forum.djangoproject.com/u/Tenmia)\
**Post date:** [February 17, 2023, 2:39pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/1 "2023-02-17T14:39:15Z")

</div>

Hi,  
I try to dump a part of my DB with the command

python manage.py dumpdata bouchons.mock --natural-foreign --natural-primary --indent 2 \> mock.json

in the terminal of pycharm and have some issues :

CommandError: Unable to serialize database: Object of type method is not JSON serializable  
Exception ignored in: \<generator object cursor\_iter at 0x000002706A332110\>  
Traceback (most recent call last):  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\db\models\sql\compiler.py”, line 1876, in cursor\_iter  
cursor.close()  
sqlite3.ProgrammingError: Cannot operate on a closed database.

The first error is for serializable, so i open the python shell form Django and run the code bellow :

> from bouchons.models import Mock
> 
> > > from django.core import serializers  
> > > q = Mock.objects.all()  
> > > test =serializers.serialize(‘json’, q, indent=2, use\_natural\_foreign\_keys=True, use\_natural\_primary\_keys=True)

And obtain the error message :  
File “”, line 1, in   
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\__init_\_.py”, line 134, in serialize  
s.serialize(queryset, \*\*options)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\base.py”, line 161, in serialize  
self.end\_object(obj)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\json.py”, line 54, in end\_object  
json.dump(self.get\_dump\_object(obj), self.stream, \*\*self.json\_kwargs)  
File “C:\Program Files\Python\Python310\lib\json\__init_\_.py”, line 179, in dump  
for chunk in iterable:  
File “C:\Program Files\Python\Python310\lib\json\encoder.py”, line 431, in \_iterencode  
yield from \_iterencode\_dict(o, \_current\_indent\_level)  
File “C:\Program Files\Python\Python310\lib\json\encoder.py”, line 405, in \_iterencode\_dict  
yield from chunks  
File “C:\Program Files\Python\Python310\lib\json\encoder.py”, line 405, in \_iterencode\_dict  
yield from chunks  
File “C:\Program Files\Python\Python310\lib\json\encoder.py”, line 438, in \_iterencode  
o = \_default(o)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\json.py”, line 106, in default  
return super().default(o)  
File “C:\Program Files\Python\Python310\lib\json\encoder.py”, line 179, in default  
raise TypeError(f’Object of type {o. **class**. **name** } ’  
TypeError: Object of type method is not JSON serializable

So during the process, the method for Foreignekey field never be convert to str. I checked this hypothesis with the debug mode (on the dump command) and i don’t understand this behavior.

 ![21B7C10A3B2046E387ACC37829E6C27F](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/b/bef07cfa66417a0c0c4eafa7fc74e46c193fbea4.png)

I look my code in the model, but i can’t see my error. Here my code write in model.

class StructureManager(models.Manager):  
def get\_by\_natural\_key(self, name):  
return self.get(name=name)

class Structure(models.Model):  
name = models.CharField(max\_length=200, unique= True)

```
objects = StructureManager()

def __str__ (self):
    return self.name

def natural_key(self):
    return (self.name,)

```

class DataflowKindManager(models.Manager):  
def get\_by\_natural\_key(self, name):  
return self.get(name=name)

class DataflowKind(models.Model):  
name = models.CharField(max\_length=200, unique= True, blank = True)

```
objects = DataflowKindManager()

def __str__ (self):
    return self.name

def natural_key(self):
    return (self.name,)

```

class Mock(models.Model):  
application = models.ForeignKey(DataflowKind, on\_delete=models.CASCADE, blank = True)  
sysEmetteur = models.CharField(max\_length=56)  
sysRecepteur = models.CharField(max\_length=56)  
version = models.CharField(max\_length=56, default=“1.0”)  
activate = ‘ON’  
deactivate = ‘OFF’  
activationChoices = [(activate, “activé”), (deactivate, “Désactivé”)]  
mockActivation = models.CharField(max\_length = 12, choices = activationChoices, default = activate)  
typeChoice = [(‘FP’, ‘Fichier plat (longueur fixe)’), (‘XML’, ‘XML’)]  
mockType = models.CharField(max\_length= 100, choices= typeChoice, default=‘FP’)  
interrupteur = [(“1”, “oui”), (“0”, “non”)]  
is\_webservice = models.CharField(max\_length= 24, choices=interrupteur)  
directionChoice = [(‘IN’, ‘Récepteur’), (‘OUT’, ‘Emetteur’)]  
mockDirection = models.CharField(max\_length= 100, choices = directionChoice, default = ‘OUT’)  
mockStructure = models.ForeignKey(Structure, on\_delete=models.CASCADE)  
creationcanevaspossible = models.CharField(max\_length=24, choices=interrupteur,  
default=“0”)  
normenomfichier = models.CharField(max\_length=100, blank=True, null=True)

```
class Meta:
    constraints = [models.UniqueConstraint(fields=['mockStructure','application',
                                                   'version'],
                                           name='unique_mock')]
def __str__ (self):
    return '%s %s %s' %(self.mockStructure.name, self.application.name, self.version)

def natural_key(self):
   return self.mockStructure.natural_key() + self.application.natural_key() + (self.version,)

natural_key.dependencies = ['bouchons.structure', 'bouchons.dataflowkind']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 17, 2023, 10:42pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/2 "2023-02-17T22:42:28Z")

</div>

I have created these models in my test environment, and I do not see the errors that you are reporting for the dumpdata command from what you have provided here so far.

This generally means one of a couple things:

- There’s more to this issue than what you’ve posted here.

- There’s some other library involved that is causing this.

- There might be something related to the database involved.

---

<div class="post-metadata">

**Author:** ![Tenmia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tenmia/32/12251_2.png) [@Tenmia](https://forum.djangoproject.com/u/Tenmia)\
**Post date:** [February 20, 2023, 2:26pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/3 "2023-02-20T14:26:35Z")

</div>

Hi,  
Thank you for that quick answer and your help.

i posted all my step and my hypothesis. I really don’t know which more informations to provide you to help to reproduce this error ^^".

For the library, my environment is :  
asgiref==3.5.0  
bcrypt==3.2.2  
certifi==2022.6.15  
cffi==1.15.0  
charset-normalizer==2.0.12  
cryptography==37.0.2  
Django==4.0.4  
django-environ==0.9.0  
django-smart-selects==1.6.0  
django-cleanup==6.0.0  
idna==3.3  
paramiko==2.10.4  
pycparser==2.21  
PyNaCl==1.5.0  
requests==2.28.0  
six==1.16.0  
sqlparse==0.4.2  
tzdata==2022.1  
urllib3==1.26.9  
lxml~=4.9.1  
setuptools~=58.1.0  
django-filter~=22.1  
pip~=22.0.4

For my database, i check with DBeaver and don’t see problem, but i’m limited and don’t know really what test can i do on it to check.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2023, 3:22pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/4 "2023-02-20T15:22:55Z")

</div>

Your settings would be the next things to check.

What are your `DATABASES` settings? (remove the password)

What are your `INSTALLED_APPS` settings?

What are your `MIDDLEWARE` settings?

---

<div class="post-metadata">

**Author:** ![Tenmia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tenmia/32/12251_2.png) [@Tenmia](https://forum.djangoproject.com/u/Tenmia)\
**Post date:** [February 20, 2023, 4:14pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/5 "2023-02-20T16:14:05Z")

</div>

the different setting:

DATABASES = {  
‘default’: {  
‘ENGINE’: ‘django.db.backends.sqlite3’,  
‘NAME’: BASE\_DIR / ‘localfile/db.sqlite3’,  
}  
}

INSTALLED\_APPS = [  
‘bouchons.apps.BouchonsConfig’,  
‘django.contrib.admin’,  
‘django.contrib.auth’,  
‘django.contrib.contenttypes’,  
‘django.contrib.sessions’,  
‘django.contrib.messages’,  
‘django.contrib.staticfiles’,  
‘django\_filters’,  
‘smart\_selects’,  
‘django\_cleanup.apps.CleanupConfig’  
]

MIDDLEWARE = [  
‘django.middleware.security.SecurityMiddleware’,  
‘django.contrib.sessions.middleware.SessionMiddleware’,  
‘django.middleware.common.CommonMiddleware’,  
‘django.middleware.csrf.CsrfViewMiddleware’,  
‘django.contrib.auth.middleware.AuthenticationMiddleware’,  
‘django.contrib.messages.middleware.MessageMiddleware’,  
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,  
]

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 20, 2023, 4:59pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/6 "2023-02-20T16:59:00Z")

</div>

Everything here looks right.

For the purposes of testing the `dumpdata` command **only** , I’d remove the `django_filters`, `smart_selects` and `django_cleanup` from your INSTALLED\_APPS - just to see what happens. So far, that’s the only differences I can see between my test environment and yours.

I would also probably want to try this directly from the command line and not from within PyCharm.

The idea behind both these steps is to just narrow the range of what might be causing this. It’s good to try and eliminate as many external factors as possible.

---

<div class="post-metadata">

**Author:** ![Tenmia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tenmia/32/12251_2.png) [@Tenmia](https://forum.djangoproject.com/u/Tenmia)\
**Post date:** [February 21, 2023, 9:32am UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/7 "2023-02-21T09:32:33Z")

</div>

I tried your suggestion, but the bug stayed the same.

 ![Sans titre](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/a/aa178d0a55b6bac26cfba0adbb7b8784429bbd73.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 21, 2023, 1:25pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/8 "2023-02-21T13:25:28Z")

</div>

I don’t know - you’ve got me on this one. I can’t recreate the issue from what’s posted here and I don’t see anything wrong with what you’re showing.

The only other possibility that comes to mind is that your models don’t properly reflect the database structure. I might try creating a new database and pointing your application to _it_. Then do a makemigration/migrate to create the tables and then try your dumpdata on the empty database.  
If that all works, then my assumption would be something is wrong with that database.

---

<div class="post-metadata">

**Author:** ![Tenmia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tenmia/32/12251_2.png) [@Tenmia](https://forum.djangoproject.com/u/Tenmia)\
**Post date:** [February 21, 2023, 3:16pm UTC](https://forum.djangoproject.com/t/raise-typeerror-fobject-of-type-o-class-name-for-serializers-serialize/18914/9 "2023-02-21T15:16:45Z")

</div>

I tried a new database. When mock table is empty i can dump, but with one single data, the same bug appear again…

and a new one appear, when i tried to load fixture with natural foreigne key, it’s not working. And it’s seem to be also a problem of converting method to iterable

File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\json.py”, line 70, in Deserializer  
yield from PythonDeserializer(objects, \*\*options)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\python.py”, line 174, in Deserializer  
obj = base.build\_instance(Model, data, using)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\base.py”, line 335, in build\_instance  
default\_manager.db\_manager(db).get\_by\_natural\_key(\*natural\_key).pk  
TypeError: bouchons.models.m\_ref.ReferenceManager.get\_by\_natural\_key() argument after \* must be an iterable, not method

The above exception was the direct cause of the following exception:

Traceback (most recent call last):  
File “D:\home\lourry\Documents\projet\bouchon-peage\manage.py”, line 22, in   
main()  
File “D:\home\lourry\Documents\projet\bouchon-peage\manage.py”, line 18, in main  
execute\_from\_command\_line(sys.argv)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\__init_\_.py”, line 446, in execute\_from\_command\_line  
utility.execute()  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\__init_\_.py”, line 440, in execute  
self.fetch\_command(subcommand).run\_from\_argv(self.argv)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\base.py”, line 414, in run\_from\_argv  
self.execute(\*args, \*\*cmd\_options)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\base.py”, line 460, in execute  
output = self.handle(\*args, \*\*options)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\commands\loaddata.py”, line 102, in handle  
self.loaddata(fixture\_labels)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\commands\loaddata.py”, line 163, in loaddata  
self.load\_label(fixture\_label)  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\management\commands\loaddata.py”, line 251, in load\_label  
for obj in objects:  
File “D:\home\lourry\Documents\projet\bouchon-peage\venvmockdev\lib\site-packages\django\core\serializers\json.py”, line 74, in Deserializer  
raise DeserializationError() from exc
