#3361: Create Database Views from the Model
--------------------------------------------+-------------------------------
Reporter: [EMAIL PROTECTED] | Owner: adrian
Status: new | Component: Database
wrapper
Version: SVN | Resolution:
Keywords: | Stage: Unreviewed
Has_patch: 0 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
--------------------------------------------+-------------------------------
Comment (by [EMAIL PROTECTED]):
''Note this is just a quick write down with a short thest of the SQL
shema, I tried to keep the formating and naming according to what in looks
like at http://www.djangoproject.com/documentation/tutorial1/#activating-
models''
An approach I could imagine would be:
{{{
from django.db import models
# Standard Database Tables...
class User(models.Model):
username = models.CharField(maxlength=200)
class Password(models.Model):
user = models.ForeignKey(User)
password = models.CharField(maxlength=30)
# The View in the Database
# note the models.View I think
# separating it would ease up
# how to create that
# (I may be totally wrong also)
class Account(models.View):
user = User.username
password = Password.password
}}}
This could result in SQL code like:
{{{
CREATE TABLE "users_user"(
id SERIAL PRIMARY KEY,
username VARCHAR(200)
);
CREATE TABLE "passwords_password"(
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES "users_user" ("id"),
password VARCHAR(30)
);
CREATE VIEW "accounts_account" AS
SELECT
"users_user"."username" AS "user",
"passwords_password"."password" AS "password"
FROM
"users_user" NATURAL JOIN "passwords_password";
}}}
Of course the `models.View` would by default do nothing (or raise an
execption) upon save or delete, I'd even think it would be an error to
wrap the save, delete, update functions in a way so that the underlying
tables are accessed. If that is wanted one could write a trigger in the
database for that and implement the standard functionality.
Please note I'm neither what I'd call a database expert or a python hacker
and I'm well aware that this example is quite simple but this is at the
moment the best I can come up with.
I guess there are quite a few people that know much better how to do this
but I find the idea of being allowed to work with views very nice...
And now that I made this example up I just figured out I have no idea
wether the underlying OR wrappers you use could even do something like
that (personally I'm especially interested in postgresql)
--
Ticket URL: <http://code.djangoproject.com/ticket/3361#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---