Basically, I have issues working with a legacy database where the tables I am using have no proper referential integrity (like no foreign keys; just tables but ‘I’ know they are related by some columns). So, Django’s framework would not be beneficial while querying n tables across m different oracle users.
Something like this:
select t1.col1, t2.col3, t3.col2 from user1.table1 t1, user2.table2 t2, user3.table3 t3 where t1.col1 = t2.col2 AND t2.col2 = t3.col3;
And now in Django’s Admin UI, I wanted to display this:
---------------------------
| col1 | col3 | col2 |
---------------------------
| abcd | defg | hijk |
---------------------------
| 1234 | 5678 | 9009 |
---------------------------
I have started on Django for its fast development only very recently. Hence, any support or docs are much appreciated.
To take advantage of Django Admin, you need to make a model first, no matter from where it fetches data.
Now, since we are mapping the model to DB, you could either make the model based on a DB view, or any one of the three tables(user1.table1, user2.table2 and user3.table3):
Base on DB view:
First, create a DB view to take
col1,col2andcol3from tables.Also, choose a primary key for the model: it could be any (including extra) column in the DB view, as long as the model field w/
primary_key=Truematches the column, here I simply choosecol1.From now on,
syncdbor Southmigratemight complain the exists, here simply ignore them.You could bypass it by faking migration in South or move the model from models.py to other files or functions that does not loaded by models.py.
After define corresponding ModelAdmin for the model
Foo, theFoocould be displayed in changelist in Django Admin.If you want to use addview or changeview to do some modification upon the tables, you could override
savemethod of theFoomodel or customize ModelForm, or customizeadd_view/change_viewof the ModelAdmin. The code varies on your actual usage, thus I don’t provide one here.Base on table user1.table1
This works similar as the method based on DB view. To be able to use addview and changeview, you need to customize ModelForm also.
Define the model
Then in admin.py
update
The usage is very uncommon, thus I’ve seen few documents about it. Most relative things could be
options.pyinsidedjango/contrib/adminand the implementation of Model insidedjango/db/models/base.py.If you want to achieve
SELECT * from t1, t2, t3 where ... like '%?%'", [var1]), the easiest way probably is to write your own view by using Admin UI, just as https://github.com/dmpayton/django-mongoadminIf
var1ofSELECT * from t1, t2, t3 where ... like '%?%'", [var1])is determined, you could make several models for each value ofvar1; if its not, you have to do some dynamic way about model and admin, just ref Django dynamic model fields , which could be even harder IMO.