PyYAML sorts keys by default, so if you use the YAML serializer that should
work for your usecase.

I think patching the JSON serializer to be deterministic by default is a
good idea, the performance cost of sorting keys is pretty small compared to
disk operations.

On 6 March 2017 at 11:32, Brice PARENT <cont...@brice.xyz> wrote:

> Hello,
>
> I've had a few customers for whom I've had to create a repository with all
> their almost-static data (pages contents, etc.). To do that, when they want
> a backup, a scripts calls several "manage.py dumpdata --indent=4
> [app].[model] > app_model.json", then commit the whole thing. The customer
> may then update their version of the repo.
>
> But whenever there are some changes, I'd like to be able to see them
> easily (that's the reason of the --indent), but right now, the fields order
> changes frequently as the order has no meaning. But in a diff, the order
> changes everything. It's almost impossible to see the changes because every
> line has moved.
>
> I have no idea if this should be an argument to dumpdata, or a special
> behaviour on the serializer's side, but having the fields sorted during the
> serialization doesn't change the validity of the data, but allows the diffs
> to be way more explicit.
>
> How it can be done for json's serializaer (django/core/serializers/json.py:60
> for django 1.8):
> json.dump(self.get_dump_object(obj), self.stream, cls=DjangoJSONEncoder,
> sort_keys=True, **self.json_kwargs)
>
> (I added the sort_keys=True argument)
>
> I haven't looked if it would have an equivalent for other serializers, nor
> if it would make any sense without the "indent" argument, for now it's just
> an idea that feels good, but probably require more thinking and advice
> before being investigated more deeply. And I didn't launch any test suite
> for now, so I don't know if there is any side effect.  Just validating the
> idea here.
>
> Any thoughts?
>
> Brice Parent
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers  (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/ms
> gid/django-developers/818b56b8-716d-d80f-ade2-1f3424206b08%40brice.xyz.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Adam

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM3AbkFeab5R0RoK63nNnbgLyN-6cHCT_eJC9SET4V6_6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to