GithubHelp home page GithubHelp logo

lingghh / django-database-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manuelnaranjo/django-database-view

0.0 0.0 0.0 26 KB

A django pluggable that allows to work with Database views

License: MIT License

Python 100.00%

django-database-view's Introduction

django-database-view

A simple pluggable application that allows to work with database views.

Quick start

  1. Install the package:

    pip install django-database-view
    
  2. In your models.py create classes which extend dbview.models.DbView like this:

    from django.db import models
    from dbview.models import DbView
    
    class ModelA(models.Model):
        fielda = models.CharField(max_length=64)
        fieldc = models.IntegerField()
    
    class MyView(DbView):
        fieldA = models.OneToOneField(ModelA, primary_key=True,
            on_delete=models.DO_NOTHING, db_column='fielda__id')
        fieldB = models.IntegerField(blank=True, null=True, db_column='fieldb')
    
        @classmethod
        def view(cls):
            """
            This method returns the SQL string that creates the view,
            in this example fieldB is the result of annotating another column
            """
            qs = modelA.objects.all(
            ).annotate(
                fieldb=models.Sum('fieldc'),
            ).annotate(
                fielda__id=models.F('pk'),
            ).order_by(
                'fielda__id',
            ).values(
                'fielda__id',
                'fieldb',
            )
            return str(qs.query)

    Alternatively get_view_str method could be used to write a custom SQL:

    class MyView(DbView):
        # ...
    
        @classmethod
        def get_view_str(cls):
            return """
                CREATE VIEW my_view AS (
                SELECT ...
            )"""
  3. Then create a migration point for your view generation, edit that migration and modify it, add: from dbview.helpers import CreateView and replace the line the call to migrations.CreateModel with CreateView.

  4. Migrate your database and start using your database views.

django-database-view's People

Contributors

andy840119 avatar manuelnaranjo avatar moorchegue avatar rainhead avatar squio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.