Serializes objects from the database.

        Works much like Django's ``manage.py dumpdata``, except that it allows you to
        limit and sort the apps that you're pulling in, as well as automatically follow
        the dependency graph to pull in related objects.

def handle(self, *app_labels, **options):
        """
        Serializes objects from the database.

        Works much like Django's ``manage.py dumpdata``, except that it allows you to
        limit and sort the apps that you're pulling in, as well as automatically follow
        the dependency graph to pull in related objects.
        """
        # TODO: excluded_apps doesnt correctly handle foo.bar if you're not using app_labels
        format = options.get('format', 'json')
        indent = options.get('indent', None)
        limit = options.get('limit', None)
        sort = options.get('sort', None)
        using = options.get('database', None)
        exclude = options.get('exclude', [])
        show_traceback = options.get('traceback', True)
        use_natural_keys = options.get('use_natural_keys', False)

        model_list = self._get_model_list(app_labels, exclude)

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        # Now collate the objects to be serialized.
        objects = []
        for model in model_list:
            if not self._can_dump_model(model, using):
                continue

            queryset = self._get_query_set(model, sort, using)[:limit]

            objects.extend(objects_from_queryset(queryset, using=using))

        objects = sort_dependencies(objects)

        try:
            return serializers.serialize(format, objects, indent=indent,
                        use_natural_keys=use_natural_keys)
        except Exception, e:
            if show_traceback:
                raise
            raise CommandError("Unable to serialize database: %s" % e)