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.
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']