Dear Fred,
Thanks a lot for the answer.
Actually I got very happy when I saw it. But I sadly found out that it does 
not work in my case. 

I think the problem is related to the way Django-1.7 behaves with respect 
to databases, migrations and so on. Not 100% sure, though.

I don't really know what to do...

Best


On Monday, November 10, 2014 4:18:27 PM UTC+1, Fred Stluka wrote:
>
> David,
>
> No.  You can use it unchanged.  With Django 1.4 at least, and I
> assume also with 1.7.
>
> I have a legacy MS SQL Server DB where I have rights to modify 
> data but not tables.  I also cannot create new DBs in MS SQL
> Server.
>
> I used *inspectdb* to create models from the existing DB.
>
> I generally have them marked as *managed=False*, but when 
> running tests, I have them *managed=True*, and I override the 
> *DATABASES* settings to point to SQLite, instead of MS SQL Server.
> Thus, when I run the tests, the test DB is created in SQLite, and
> the real DB server is untouched.
>
> Here's how I do it:
>
> *settings.py:*
>
> DATABASES = { ... the usual stuff ... }
>
> # Decide whether we're running unit tests
> RUNNING_UNIT_TESTS = 'test' in sys.argv
>
> if RUNNING_UNIT_TESTS:
>     DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', }
>
> *models.py:*
>
>     class Meta:
>         managed = True if settings.RUNNING_UNIT_TESTS else False
>
> --Fred 
> ------------------------------
> Fred Stluka -- mailt...@bristle.com <javascript:> -- 
> http://bristle.com/~fred/ 
> Bristle Software, Inc -- http://bristle.com -- Glad to be of service! 
> Open Source: Without walls and fences, we need no Windows or Gates. 
> ------------------------------
>  On 11/10/14 9:43 AM, dpalao...@gmail.com <javascript:> wrote:
>  
> 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<span 
>
> ...

-- 
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/a8a21dea-8808-4942-a75d-de4763b0a754%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to