Django: Query returns different results in management command
Question: I have a django application with a model named `TestCase`. There are 9
instances of the model currently stored in the DB, which I can see by running
`TestCase.objects.all()` in the shell, and they're also being displayed
correctly in my views.

However, in a management command I'm running, the same query
(`TestCase.objects.all()`) consistently returns an empty list instead. I have
imported the model correctly, and the management command is even able to add
entries to the database without any problem, so reading back from the database
shouldn't be a problem.

Any ideas on what could be causing this?

Some context: The django app is a frontend to display and manage testcases.
The management command reads in the results from the test into the DB. I need
to access the DB in the management command to incorporate test runs into the
app - if a testcase provides a test run (an integer) it is used, but if it
does not, then the command sets the test run to one plus the max test run
present in the app already - this is where I need to access the DB (using
something like `TestCase.objects.all().aggregate(Max('test_run'))`).

I'm using Django 1.4.

This is the management command:

    
    
    from django.core.management.base import NoArgsCommand
    from django.core.management.base import AppCommand, CommandError
    from mainapp.models import TestCase
    from django.utils import timezone
    from django.db.utils import IntegrityError
    from django.conf import settings
    from django.core import management
    from django.db.models import Max 
    
    import cPickle
    import errno
    
    class Command(NoArgsCommand):
      def handle_noargs(self, **options):
        management.call_command('reset', 'mainapp', interactive=False) 
        print "ALL: %s" % TestCase.objects.all()
        self.traverse()
    
      def traverse(self):
        ...
    

the output is `ALL: []`. I've omitted the source of the `traverse()` method,
but the problem is visible before that, so it shouldn't impact anything.

Here's the output from the shell showing the instances in the DB:

    
    
    [as@as-mac  ui]$ pm shell
    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from mainapp.models import TestCase
    >>> TestCase.objects.all()
    [<TestCase: internet explorer 8 on WIN7 at https://www.google.com/ >, <TestCase:     internet explorer 8 on WIN7 at https://www.google.com/ >, <TestCase: internet explorer 8 on WIN7 at https://www.google.com/search?q=mooo >, <TestCase: internet explorer 8 on WIN7 at https://www.google.com/search?q=mooo >, ...]
    >>> TestCase.objects.count()
    454
    

Please feel free to ask for more details!


Answer: The following line of your Command is resetting the database and clearing
data:

    
    
    management.call_command('reset', 'mainapp', interactive=False) 
    

If you remove this line, the record count in the shell and command will be
equivalent.

