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 -- mailto:f...@bristle.com -- 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.pyt...@gmail.com 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:

|
classJobInfo(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()

classMeta:
        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):

|
classManagedModelDiscoverRunner(DiscoverRunner):
defsetup_test_environment(self,*args,**kwargs):
fromdjango.db.models.loading importget_models
self.unmanaged_models =[m form inget_models()
ifnotm._meta.managed]
form inself.unmanaged_models:
            m._meta.managed =True
print("setting %s._meta.managed to True"%(m.__name__,))
super(ManagedModelDiscoverRunner,self).setup_test_environment(*args,**kwargs)

defteardown_test_environment(self,*args,**kwargs):
super(ManagedModelDiscoverRunner,self).teardown_test_environment(*args,**kwargs)
# reset unmanaged models
form inself.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:

|
classManagedModelDiscoverRunner(DiscoverRunner):
defsetup_databases(self,**kwargs):
fromdjango.db.models.loading importget_models
self.unmanaged_models =[m form inget_models()ifnotm._meta.managed]
form inself.unmanaged_models:
            m._meta.managed =True
print("setting %s._meta.managed to True"%(m.__name__,))
returnsuper(ManagedModelDiscoverRunner,self).setup_databases(**kwargs)

defteardown_databases(self,old_config,**kwargs):
super(ManagedModelDiscoverRunner,self).teardown_databases(old_config,**kwargs)
# reset unmanaged models
form inself.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 <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto: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 <https://groups.google.com/d/msgid/django-users/bbcc092a-7530-448d-a681-c01fbae742e9%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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/5460D70F.6050100%40bristle.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to