Skip to main content

A small package for Django to ease the creation of temporary tables, based on model definitions and querysets

Project description

Author:

Keryn Knight

Version:
0.1.2

A small package for Django to ease the creation of database temporary tables.

It doesn’t need to be in your INSTALLED_APPS

Usage

Define a mostly normal Django model, like so:

from django.db import models

class MyCoolModel(models.Model):
    value = models.PositiveIntegerField(primary_key=True)

    class Meta:
        abstract = True
        managed = False

Pay special attention to the Meta attributes. It’ll complain otherwise.

Provide a definition for the temporary table somewhere:

from storedqueries import TemporaryTable

class MyTemporaryTable(TemporaryTable):
    model = MyCoolModel
    queryset = Somedata.objects.order_by('?').annotate(value=models.F('key_name')).values_list('value').iterator()

Make use of the temporary table:

from django.http import JsonResponse

def myview(request, *args, **kwargs):
    with MyTemporaryTable() as TemporaryModel:
        keys = TemporaryModel.objects.all()
        data = tuple(Somedata.objects.filter(key_name__in=keys))
        return JsonResponse({'values': data})

Using the with my_cls() as thing: syntax will create a uniquely named temporary table using the queryset connection and data to fill it, when the with scope closes, the temporary table is dropped. The TemporaryModel variable will be a subclass of MyCoolModel bound to the unique name for the temporary table.

If you have a query which cannot be defined at module scope, you can do:

class MyTemporaryTable(TemporaryTable):
    model = MyCoolModel
    def source_queryset(self):
        return Somedata.objects.filter(created__lte=timezone.now()).annotate(value=models.F('key_name')).values_list('value').iterator()

If you still cannot get the query correct, because it has a dependency on something like request.user etc, you can do:

def myview(request, *args, **kwargs):
    qs = Somedata.objects.filter(user=request.user.pk)
    with MyTemporaryTable(queryset=qs) as TemporaryModel:
        raise NotImplementedError("Dynamic queryset binding")

or probably even:

def myview(request, *args, **kwargs):
    qs = Somedata.objects.filter(user=request.user.pk)
    with TemporaryTable(model=MyCoolModel, queryset=qs) as TemporaryModel:
        raise NotImplementedError("Dynamic model AND queryset binding")

The license

It’s FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2019, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-storedqueries

0.1.2

  • Initial release

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-storedqueries-0.1.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_storedqueries-0.1.2-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-storedqueries-0.1.2.tar.gz.

File metadata

  • Download URL: django-storedqueries-0.1.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for django-storedqueries-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6a88669e5b67160740f9fae6a1e61a9bdc8816f009fb9ed9aeaa025795b16636
MD5 fca5637f5931ab0dde034de5ea98eadb
BLAKE2b-256 abe580f725f97f49bf61a048f02d99c38c140c49dc95c1a7f43ad08d0065fb75

See more details on using hashes here.

File details

Details for the file django_storedqueries-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_storedqueries-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for django_storedqueries-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a64a827b20ae7bd6cf97bf441be0d67deb31f1213b97c4b4212615063322f2f8
MD5 68a19896a008d277c801f4f921702112
BLAKE2b-256 b931f297a8cc879aeb35e21e1ba591db4e05ee3a14db3e3605a16c1be58ae214

See more details on using hashes here.

Supported by

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