Skip to main content

A simple Django app to handle database views.

Project description

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

So far only MySQL is supported as backend, but more could be added if necessary.

Quick start

  1. In your models.py create classes which extend dbview.DbView like this:

    from django.db import models
    from dbview.models import DbView
    
    class ModelA(models.Model):
        fielda = models.CharField()
        fieldc = models.IntegerField()
    
    class MyView(DbView):
        fieldA = models.OneToOneField(ModelA, primary_key=True,
            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:

```python

class MyView(DbView):

    # ...

    @classmethod
    def get_view_str(cls):
        return """
        CREATE VIEW my_view AS (
            SELECT ...
        )
        """
```
  1. 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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-database-view-0.2.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

django_database_view-0.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django-database-view-0.2.0.tar.gz.

File metadata

  • Download URL: django-database-view-0.2.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for django-database-view-0.2.0.tar.gz
Algorithm Hash digest
SHA256 795c54d780fa5b6c13da1f7adf7c8e1b710329e64eed36e5ba514eda4d0148b4
MD5 9c99b2b6e8eb059ca0daaf53f293ab3d
BLAKE2b-256 62ec7534a27839c1333497f7f04239ae58fbfbfa4c77a191968829fb4c8be031

See more details on using hashes here.

File details

Details for the file django_database_view-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_database_view-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for django_database_view-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58f061b02ab7ea17c280ecf310320c39b77dedfbb60659c81dfae0349b3301e3
MD5 859f281180b5a40d085ca454c35a3ea7
BLAKE2b-256 b5d29508c25e09f545e600e71568f1c17bb3ec4713f4133ad805135c0fa9642e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page