﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21075	Doc improvement for using call_command with arguments	nahumoz@…	Tim Graham	"Hi Everyone, 

I would like to created a backup of my models when new model elements are created, therefore I 
defined in my `models.py` the following function:
{{{
    def save_models_as_json(**kwargs):
        with open(JSON_DATA_FILE, 'w') as j:
            management.call_command('dumpdata', 'TimePortal',
                                    indent=4, stdout=j)

    # later in my models file I register it with:
    post_save.connect(save_models_as_json, sender=Employee)
}}}
The above command is working perfectly fine when I use the admin GUI I find
inside the `JSON_DATA_FILE` all my relevand information from my application
TimePortal.
This function also works fine when running unit tests with:
{{{
    $ python mange test TimePortal
}}}

Things get ugly when I want the dumpdata command to include some more
arguments, e.g:

{{{
    def save_models_as_json(**kwargs):
        with open(JSON_DATA_FILE, 'w') as j:
            management.call_command('dumpdata', 'TimePortal', '--natural'
                                    indent=4, stdout=j)
}}}
or:
{{{
    def save_models_as_json(**kwargs):
        with open(JSON_DATA_FILE, 'w') as j:
            management.call_command('dumpdata', 'TimePortal', '-e', 'contenttypes',
                                    indent=4, stdout=j)
}}}
When I run my unit tests, django will crash, with either:
{{{
     $ python ../manage.py test TimePortal
    Creating test database for alias 'default'...
    ........Error: Unknown application: --natural
    E............
    ======================================================================
    ERROR: test_customer_has_employee (TimePortal.test_models.TestModels)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File ""/home/ozn/software/zeitport/zeitportal/TimePortal/test_models.py"", line 18, in setUp
        worksfor=self.customer)
      File ""/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py"", line 134, in get_or_create
        return self.get_query_set().get_or_create(**kwargs)
      File ""/usr/local/lib/python2.7/dist-packages/django/db/models/query.py"", line 452, in get_or_create
        obj.save(force_insert=True, using=self.db)
      File ""/usr/local/lib/python2.7/dist-packages/django/db/models/base.py"", line 463, in save
        self.save_base(using=using, force_insert=force_insert, force_update=force_update)
      File ""/usr/local/lib/python2.7/dist-packages/django/db/models/base.py"", line 565, in save_base
        created=(not record_exists), raw=raw, using=using)
      File ""/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py"", line 172, in send
        response = receiver(signal=self, sender=sender, **named)
      File ""/home/ozn/software/zeitport/zeitportal/TimePortal/models.py"", line 61, in save_models_as_json
        'dumpdata', '--natural', 'TimePortal', indent=4,  stdout=j)
      File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 150, in call_command
        return klass.execute(*args, **defaults)
      File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 249, in execute
        sys.exit(1)
    SystemExit: 1

    ----------------------------------------------------------------------
    Ran 21 tests in 0.017s

    FAILED (errors=1)
    Destroying test database for alias 'default'...
}}}
or with 
{{{   
   $ python ../manage.py test TimePortal
    Creating test database for alias 'default'...
    ........Error: Unknown application: -e
    E............
    ======================================================================
      
      ... chopped for sanity ...
      File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 150, in call_command
        return klass.execute(*args, **defaults)
      File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 249, in execute
        sys.exit(1)
    SystemExit: 1

    ----------------------------------------------------------------------
    Ran 21 tests in 0.017s

    FAILED (errors=1)
    Destroying test database for alias 'default'...

}}}
Thanks for the excellent django documentation, I found out I could acchieve 
the desired serialization without using `management.call_command`:
{{{
    from django.core import serializers
    JSONSerializer = serializers.get_serializer('json')
    json_serializer = JSONSerializer()

    def save_models_as_json(**kwargs):
        with open(JSON_DATA_FILE, 'w') as j:
            json_serializer.serialize(list(Employee.objects.all())
                                      + list(Customer.objects.all())
                                      +list(Rules.objects.all())
                                      + list(Account.objects.all()),
                                      indent=4, use_natural_keys=True)
            j.writelines(json_serializer.getvalue())

}}}
despite the above 'solution', I think this is worth the attention of the
developers, since it might give a hint for some problem with
management.call_command.

I am using django 1.5.2 and Python 2.7.5. 
I also tested this with Python 2.6.8 and django 1.4.5

Thanks for reading so far. Hopefully, you can find a good explanation to 
this behavior with multiple options passed to `management.call_command`.
"	Cleanup/optimization	closed	Documentation	dev	Normal	fixed		bmispelon@…	Accepted	1	0	0	0	0	0
