Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding django loaddata or right way to dumpdata for fixutes

Tags:

python

django

I have serialized 4 apps from my project with manage.py dumpdata. But when I'm trying to load it in test as a fixture - i'm getting "Validation Error : This value must be True or False".

How to understand, which line of dump is wrong? Or in which model I got this error?

UPD: Problem is coming from invalid fixture. But I can't understand, why django dumbdata creates invalid fixtures.

I have added

   print field.name

in django.core.serializers.python and found invalid values manually. But it's not good way.

UPD. I'm still interested in a way to dump data for any model and reuse it in tests as a fixture. I'm using postgresql for development.

like image 234
Nikolay Fominyh Avatar asked Jan 01 '26 10:01

Nikolay Fominyh


1 Answers

First of all are you using any custom/3rd party Fields?

Although if you did and django's dumpdata couldn't serialize it, it would raise an exception I suppose. Anyway since last time I checked there was not any sufficient documentation for writing a custom django serializer, here is an example for serializing a custom UUIDField:

from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.json import DjangoJSONEncoder

class CustomJSONEncoder(DjangoJSONEncoder):
    def default(self, obj):
        if isinstance(obj, uuid.UUID):
            return obj.hex
        return super(CustomJSONEncoder, self).default(obj)

class Serializer(PythonSerializer):
    internal_use_only = False
    def end_serialization(self):
        json.dump(self.objects, self.stream, cls=CustomJSONEncoder, **self.options)

    def getvalue(self):
        if callable(getattr(self.stream, 'getvalue', None)):
            return self.stream.getvalue()

Then in your settting.py add

SERIALIZATION_MODULES = { 'myjson' : 'path.to.my.module.with.serializer' }

Then from your shell you can do

python manage.py dumpdata --format myjson > myfixtures.myjson # the extension is important!

You can load it with

python manage.py loaddata myfixtures.myjson

Strangely enough loaddata does not accept a "format" parameter like dumpdata. It decides the format based on the file extension. Yet I could not find a way to hook my custom encoder, so I had to write a "new" serializer :(

That's what I did months ago when I needed to write a custom serializer and I had to dig through the django's source code to figure it out, since the official docs had nothing on the subject. I hope it provides some help.

like image 91
rantanplan Avatar answered Jan 02 '26 23:01

rantanplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!