#21075: django crashes with sys.exit(1) when management.call_command is used 
with
unittest
--------------------------------------+--------------------
     Reporter:  nahumoz@…             |      Owner:  nobody
         Type:  Bug                   |     Status:  new
    Component:  Core (Serialization)  |    Version:  1.5
     Severity:  Normal                |   Keywords:
 Triage Stage:  Unreviewed            |  Has patch:  0
Easy pickings:  0                     |      UI/UX:  0
--------------------------------------+--------------------
 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`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21075>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/060.e013f079f12d0ac24d5b04b08aad2863%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to