Conversation
There are some cases where it would be nice to generate the SQL of a view, and this isn't possible if the SQL must be defined on the class because not all apps have loaded at that point. This change checks if the View class has defined a 'generate_sql' classmethod and if they have it uses that to get the sql for that view class
Contributor
|
Hey @adamhaney this looks really good! Could you add some docs for this too please? I did have one question, would it have been easier to just define an If you can include a use-case in the docs, that would help clarify this case :) |
This setting allows a dev to disable the automatic syncing behavior. I've encountered a situation in a project's continuous integration where the post migration signal is run before some of the tables the view refers to have been created/migrated. This only happens on a brand new environment and most glaringly on test servers that might not even run all the migrations for unit tests.
|
I needed this, so added it to my fork - see https://github.com/mikicz/django-pgviews and https://pypi.org/project/django-pgviews-redux/. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have a use case where I'm joining several models into a unified View, and I'd like for the view to include all of the fields from all of the tables (and to update automagically if any of the models have new columns added).
It's possible for me to generate the columns for my SELECT query using a list comprehensions and a model's ._meta.get_fields() method, however, this method fails if the models haven't been loaded into Django yet, so I can't just call .format against my SQL string when defining the View class' sql string.
This changeset allows a view to define a class method
generate_sqlon a view, if that classmethod is present then it is used to get the sql which is used to define the view instead of the class sql property.