예제 #1
0
    def handle(self, *app_labels, **options):
        excluded_apps = [get_app(app_label) for app_label in options['exclude'] if "." not in app_label]
        excluded_models = [model.split('.') for model in options['exclude'] if "." in model]
        
        if len(app_labels) == 0:
            app_list = [app for app in get_apps() if app not in excluded_apps]
        else:
            app_list = [get_app(app_label) for app_label in app_labels]

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        if options['format'] not in serializers.get_public_serializer_formats():
            raise CommandError("Unknown serialization format: %s" % options['format'])

        try:
            serializers.get_serializer(options['format'])
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % options['format'])

        objects = []
        models = []
        for app in app_list:
            app_name = app.__name__.split('.')[-2] # assuming -1 is 'models' and -2 is name
            models.extend( [model for model in get_models(app) if [app_name, model.__name__] not in excluded_models] )
        models = foreign_key_sort(models)

        for model in models:
            objects.extend(model._default_manager.all())
        
        try:
            print serializers.serialize(options['format'], objects, indent=options['indent'])
        except Exception, e:
            if options['traceback']:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
예제 #2
0
    def handle(self, *app_labels, **options):
        excluded_apps = [get_app(app_label) for app_label in options["exclude"] if "." not in app_label]
        excluded_models = [model.split(".") for model in options["exclude"] if "." in model]

        if len(app_labels) == 0:
            app_list = [app for app in get_apps() if app not in excluded_apps]
        else:
            app_list = [get_app(app_label) for app_label in app_labels]

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        if options["format"] not in serializers.get_public_serializer_formats():
            raise CommandError("Unknown serialization format: %s" % options["format"])

        try:
            serializers.get_serializer(options["format"])
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % options["format"])

        # Output
        output_file = options.get("output")
        if output_file is None:
            output = sys.stdout
        else:
            output = open(output_file, "w")

        objects = []
        models = []
        for app in app_list:
            app_name = app.__name__.split(".")[-2]  # assuming -1 is 'models' and -2 is name
            models.extend([model for model in get_models(app) if [app_name, model.__name__] not in excluded_models])
        models = foreign_key_sort(models)

        for model in models:
            objects.extend(model._default_manager.all())

        try:
            print serializers.serialize(
                options["format"], objects, indent=options["indent"], use_natural_keys=options["use_natural_keys"]
            )
        except Exception, e:
            if options["traceback"]:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
예제 #3
0
 def models_with_subclasses(models):
     for model in models:
         models.extend(models_with_subclasses(model.__subclasses__()))
     return models
예제 #4
0
 def models_with_subclasses(models):
     for model in models:
         models.extend(models_with_subclasses(model.__subclasses__()))
     return models