Skip to main content

Automatic creation and managment of QuerySet based DB Views

Project description

*UNDER ACTIVE DEVELOPMENT*

An app for creating and maintaining DB views based on ORM QuerySets

This application is especially useful for when you want to tie analytics tools such as tableau, etc directly into your backend without having to do all the SQL. You can create the views required directly from the django ORM.

django-qs-views is a reusable, installable app for use with a Django project. Please note that using this package will result in the direct manipulation of your Django project’s database.

Installation:

  1. Install django-qs-views

    pip install django-qs-views
  2. Add qs_views to your project setting’s INSTALLED_APPS. You must also have contenttypes framework installed

    INSTALLED_APPS = [
        ...
        'django.contrib.contenttypes',
        'qs_views',
    ]
  3. Migrate your database

    python manage.py migrate

Example Usage:

Say your project has the following models.

class Organization(models.Model):
    name = models.CharField(max_length=250)
    country_code = models.CharField(max_length=5, null=True)

class Person(models.Model):
    org = models.ForeignKey('Organization', null=True, blank=True, on_delete=models.SET_NULL)
    last_name = models.CharField(max_length=100, null=True)
    first_name = models.CharField(max_length=100, null=True)
    middle_name = models.CharField(max_length=100, null=True, blank=True)
    salary = models.FloatField(null=True)

    @classmethod
    def get_person_view_qs(cls):
        return cls.objects.annotate(
            org_name = F('org__name')
        ).values()

You would like to create a DB view from the Person model that joins Org info. Simply create a @classmethod that generates the queryset. In the example above the method is called `` get_person_view_qs ``

To generate the view use the ORM (or create UI) to create a QsView instance and call the create_view method

content_type = ContentType.objects.get_for_model(Person)
dbv = QsView.objects.create(
    view_name='person_view',  content_type=content_type,
    get_qs_method_name = 'get_person_view_qs',
    materialized=False,  db_read_only_users=['user_readonly1'],
)
dbv.create_view()

At this point the default DB will have a view in called “person_view” that matches the result of the queryset returned from get_person_view_qs. If you delete the QsView instance the view will be dropped from the database.

Contributing:

Developers will need to install packages from requirements.txt. Linux users: You’ll need to use psycopg2’s binaries as pip doesn’t seem able to install psycopg2 from source. After running

pip install -r requirements.txt, linux users must also run

pip install psycopg2-binary

A test suite is provided for vetting commits prior to integration with the project. The test suite will require several environment variables and a postgres test database in order to function properly.

Set up tests:

Create a database on your postgres server called QsViews (or if you use another name set it as the DB_NAME environment variable.)

sudo -u postgres createdb QsViews
sudo -u postgres psql
grant all privileges on database QsViews to postgres;

Likewise, if you are using a postgres user other than postgres set the name of this user as DB_USER. The environment variable DB_HOST must point to your postgres server, if the default, localhost is not appropriate, change it. DB_PASSWORD will be used for postgres credentials and DB_PORT for the port (default 5432.)

Running tests:

Run the following command to initiate the test runner and run the test suite:

python runtests.py

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-qs-views-0.0.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

django_qs_views-0.0.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django-qs-views-0.0.3.tar.gz.

File metadata

  • Download URL: django-qs-views-0.0.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for django-qs-views-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2e83726c49650785a5f8333e8035de1312a1786aeb2617ed53d3f7225e35cfba
MD5 af0e9d506aae96ce5ad44be67608c843
BLAKE2b-256 66a718897628a87ecac65319bd6dc1961337aecd81a128aaebcc999a0089bb64

See more details on using hashes here.

File details

Details for the file django_qs_views-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_qs_views-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8c56c0a16dc0162c5e4348cb0f21d20b11f9f886dcae60f1d05f5fd122b2d4c7
MD5 2172262650456ed641da702ce9bf6ab8
BLAKE2b-256 88dff58fa27e76608be6113790057cc6a15d7aaa666a8ab3175c0da92821d100

See more details on using hashes here.

Supported by

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