Hi, I'm writing a Django application that uses an existing database. If I understood it well, in such a case one must create non-managed models for the legacy tables to avoid Django creating already existing tables, right? For instance, this is how one of my models looks like:
class JobInfo(models.Model): job_db_inx = models.IntegerField(primary_key=True) id_job = models.IntegerField() id_user = models.IntegerField() id_group = models.IntegerField() account = models.TextField(blank=True) cpus_req = models.IntegerField() cpus_alloc = models.IntegerField() nodelist = models.TextField() nodes_alloc = models.IntegerField() partition = models.TextField() time_start = models.IntegerField() time_end = models.IntegerField() was_updated = models.IntegerField() jobmondatacleared = models.IntegerField(db_column='jobMonDataCleared') # Field name made lowercase. endupcount = models.IntegerField(db_column='endUpCount') # Field name made lowercase. approved = models.IntegerField() class Meta: managed = False db_table = 'job_info' The problem, of course happens when the unit tests are run. The test database must be created when the tests start. But because "managed = False", the tables are not created. First round. Googling a bit I found a recipe to by-pass this problem: modify the DiscoverRunner class (I found it in here <http://www.caktusgroup.com/blog/2010/09/24/simplifying-the-testing-of-unmanaged-database-models-in-django/>, and tailor it trvially to remove the deprecation warning): class ManagedModelDiscoverRunner(DiscoverRunner): def setup_test_environment(self, *args, **kwargs): from django.db.models.loading import get_models self.unmanaged_models = [m for m in get_models() if not m._meta.managed] for m in self.unmanaged_models: m._meta.managed = True print("setting %s._meta.managed to True" % (m.__name__,)) super(ManagedModelDiscoverRunner, self).setup_test_environment(*args , **kwargs) def teardown_test_environment(self, *args, **kwargs): super(ManagedModelDiscoverRunner, self).teardown_test_environment(* args, **kwargs) # reset unmanaged models for m in self.unmanaged_models: m._meta.managed = False So I created a directory in my project called "tests" and put the above code in a file called "managed_runner.py" in there. To be more explicit, the tree looks like: |-- myapp | |-- admin.py | |-- __init__.py | |-- models.py | |-- templates | | `-- myapp | | `-- home.html | |-- tests.py | `-- views.py |-- myproj | |-- __init__.py | |-- settings.py | |-- urls.py | `-- wsgi.py |-- manage.py `-- tests |-- __init__.py `-- managed_runner.py Then I modified my "settings.py" file adding TEST_RUNNER="tests.managed_runner.ManagedModelDiscoverRunner" to it. And I ran the tests. Still, it fails: django.db.utils.ProgrammingError: Table 'test_db.job_info' doesn't exist But I see the output of print in the screen saying that the models' ._meta.managed attributes are set to True. Second round. Reading the Django docs, I see that DiscoverRunner has a couple of interesting methods: setup_databases and teardown_databases. I tried to subclass the DiscoverRunner again, this time it looks like: class ManagedModelDiscoverRunner(DiscoverRunner): def setup_databases(self, **kwargs): from django.db.models.loading import get_models self.unmanaged_models = [m for m in get_models() if not m._meta. managed] for m in self.unmanaged_models: m._meta.managed = True print("setting %s._meta.managed to True" % (m.__name__,)) return super(ManagedModelDiscoverRunner, self).setup_databases(** kwargs) def teardown_databases(self, old_config, **kwargs): super(ManagedModelDiscoverRunner, self).teardown_databases( old_config, **kwargs) # reset unmanaged models for m in self.unmanaged_models: m._meta.managed = False and I get exactly the same error. What am I missing? Thanks in advance, David PS Django-1.7 with Python-3.3 -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bbcc092a-7530-448d-a681-c01fbae742e9%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.