Skip to content

Instantly share code, notes, and snippets.

@mattmcc
Created March 27, 2013 00:24
Show Gist options
  • Save mattmcc/5250561 to your computer and use it in GitHub Desktop.
Save mattmcc/5250561 to your computer and use it in GitHub Desktop.
Quick & dirty "read-only" model for using SQL views
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
@bolshoibooze
Copy link

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment