Skip to main content
Author:

Keryn Knight

Version:
0.1.4

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.4

  • Changed the open and close methods of TemporaryTableEditor to avoid using the cursor as a context manager for greater compatibility (ie: I have an old internal project where this could be useful)

0.1.3

  • Raise an exception at runtime if the temporary model being passed in delares ForeignKey etc without setting related_name="+" for each.

  • Add functionality for MySQL to detect if it can use the MEMORY engine.

0.1.2

  • Initial release

Metadata

Release files for django-storedqueries 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-storedqueries 0.1.4
File Size Uploaded
django-storedqueries-0.1.4.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-storedqueries 0.1.4
File Interpreter ABI Platform
django_storedqueries-0.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.5 kB

Release files / django-storedqueries-0.1.4.tar.gz

Download URL django-storedqueries-0.1.4.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
aed8248e10fa30995b499f1114af96997f4b7c04d1205efa93ede0e8d532f49d
BLAKE2b-256 checksum
How to use checksums
274a8281329803db5a120aca036d78128b4ae0c99442aca41119de002818e6f3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / django_storedqueries-0.1.4-py2.py3-none-any.whl

Download URL django_storedqueries-0.1.4-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ceeb0849ede01c80a0298b60bd39786bb662a44c849c0e50ebb4ec132035fe44
BLAKE2b-256 checksum
How to use checksums
eb930b10bee394e9e1218a5f84a4fe161cf4b13cf2a24df986b1f193260f1866
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page