Login

Quick and dirty way to make dynamically available all tables of a database as models

Author:
henning
Posted:
May 27, 2007
Language:
Python
Version:
.96
Tags:
sql models database inspectdb automagic
Score:
1 (after 1 ratings)

There are a lot of reasons why you should not do this. But nevertheless it might be useful in certain situations.

If your database does not change and you want to use validators etc you better use inspectdb from django-admin.py

1
2
3
4
5
6
7
#Put this in yourproject/yourtestapp/models.py

from django.core.management import inspectdb

code = "\n".join(inspectdb())
code = code.replace("    class Meta:", "    class Admin: pass\n    class Meta:")
exec code in globals()

More like this

  1. Models with database views by diverman 6 years, 1 month ago
  2. Save a model using an arbitrary db connection by weswinham 5 years, 11 months ago
  3. Database template loader by doomatel 3 years, 4 months ago
  4. Pickled Object Field by obeattie 7 years, 7 months ago
  5. Custom 'save' method, automate creating pdf file from entry. by I159 3 years, 10 months ago

Comments

Please login first before commenting.