Created
March 27, 2013 00:24
-
-
Save mattmcc/5250561 to your computer and use it in GitHub Desktop.
Quick & dirty "read-only" model for using SQL views
This file contains 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
class ViewManager(models.Manager): | |
def bulk_create(self, *args, **kwargs): | |
raise NotImplementedError | |
def create(self, *args, **kwargs): | |
raise NotImplementedError | |
def get_or_create(self, *args, **kwargs): | |
raise NotImplementedError | |
def delete(self, *args, **kwargs): | |
raise NotImplementedError | |
def update(self, *args, **kwargs): | |
raise NotImplementedError | |
class View(models.Model): | |
objects = ViewManager() | |
class Meta: | |
abstract = True | |
managed = False | |
def delete(self, *args, **kwargs): | |
raise NotImplementedError | |
def save(self, *args, **kwargs): | |
raise NotImplementedError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe you should just use this https://github.com/isotoma/django-postgres. Creating a model view is as simple as:
class Book(models.Model):
title = models.CharField()
author = models.ForeignKey()
class Meta:
db_table = 'Book'
verbose_name_plural = 'Books'
class vBook(pg.View):
projection = [ 'myapp.Book']
sql = MY_RAW_SQL #a pre-defined raw sql
(pardon the horrible comment formatting, but i bet you can get the general idea)
And that's it !