I recently tried to create a custom SQL view on my Django-powered site
and use it via usual Django ORM approach; I created a view and then
manually built the Django model resembling this view, but quickly
noticed that any attempt to request Django to regenerate the Django-
generated tables causes a try to DROP this view as if it was a table,
and then create a new table with the same name.
My opinion is that the most trivial way to let Django-users use the
custom SQL views in a Django-way and at the same time allow them to
automatically manage the SQL tables by Django is to let them specify
that particular models do not need any SQL actions for them during
usual sqlall/sqlreset/reset/etc manage commands.
I raised a ticket #5858 for it and applied a quick patch which works
for me and hopefully covers all the places where an SQL could be
generated for such model. This patch creates a new Meta option called
sql_generation_required (name is just tentative) which is set to True
by default for any models (what stands for the existing behaviour) but
the user may change it to False, and then this model will not be
touched by any manage.py SQL commands.
But note that this patch does not alter the behaviour of sqlcustom; if
the user does not want sqlcustom to touch the view, one should remove
the custom SQL files rather than mark the model with Meta option.
With best regards,
Alex Myodov
Patch propagated to trunk Django version (original patch was for 0.9.6
release) and resubmitted under the #3163 ticket originally raising
this problem (ticket #5858 was closed as a duplicate to #3163).
Any comments on the approach (good idea or not? is any "more djangy"
approach to handle SQL views is planned already?), as well as testing
(especially in the area of many-to-many tables) are appreciated.
On 2 нояб, 00:27, Honeyman <amyo...@gmail.com> wrote:
...
> My opinion is that the most trivial way to let Django-users use the
> custom SQL views in a Django-way and at the same time allow them to
> automatically manage the SQL tables by Django is to let them specify
> that particular models do not need any SQL actions for them during
> usual sqlall/sqlreset/reset/etc manage commands.
...
But I do want to add my support behind any effort to allow Django
models to integrate easily with SQL views. I think that this can be
another useful way to integrate custom SQL code into a Django app
where the Django ORM is not up to the task.
It also separates the custom SQL code from the Django app code, which
is a "clean" approach.
With PostgreSQL's re-write rules, I can imagine this becoming a very
powerful technique for those cases thatORM was not planned to handle.
After all, an SQL view in PostgreSQL is just a simple re-write rule
that is created automatically for you when you execute "CREATE
VIEW ...". If views can be supported by Django, there is nothing
stopping users from writing more extensive re-write rules to support
even "updatable views". In this way, views can also be used for
creating/updating records in addition to just querying for them.
All this, of course, is only recommended for those that are qualified
and comfortable to be working with SQL views and PostgreSQL re-write
rules in the first place.
Jeff
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."
The name is silly. Call it create_db_table and have it default to True.
For the (very few) users who need this functionality, they can set it to
False and it won't generate any SQL for that model. That's all that's
needed here.
Tests will be very painful and I wouldn't worry about it too much, since
it's such an edge case and will just add overhead. If you run
"./manage.py sql <model_name>" and see any output, it doesn't work.
That's really all that's required there.
Keep in mind, though, that at that point, might still take a bit of time
to get final review and then be committed. There are hundreds of open
tickets in the system, many of much greater impact on the user base than
this one. We're working towards getting a 1.0 release out the door and
need to focus a lot of energy on the features that impact 100% of the
userbase at the temporary expense of those that impact 1%. That's the
way life works. So get the patch up to as good as it can get and people
who really, really need the feature can apply hte patch themselves and
we'll get it into the tree at some point when we have a moment.
Regards,
Malcolm
--
Remember that you are unique. Just like everyone else.
http://www.pointy-stick.com/blog/
Meh.
We don't really do a lot of "management", so I slightly prefer the
fairly intuitive "create" over "manage". If you use manage as you
intend, it opens the big can of worms about what 'manage' means and we
already have enough tickets opened to resolve relatively trivial points
that I'm not interested in opening up that particularly slippery slope
for skiing.
You can call it manage_db_table in your patch if you like. If I commit
it, I'll probably change that, but it's really neither here nor there.
Malcolm
--
A conclusion is the place where you got tired of thinking.
http://www.pointy-stick.com/blog/