Package: django-storedqueries
Author:Keryn KnightVersion:0.1.4A small package forDjangoto ease the creation of database temporary tables.It doesn’t need to be in yourINSTALLED_APPSUsageDefine 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 = FalsePay special attention to theMetaattributes. 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 thewith my_cls() as thing:syntax will create a uniquely named
temporary table using thequerysetconnection and data to fill it,
when thewithscope closes, the temporary table is dropped. TheTemporaryModelvariable will be asubclassofMyCoolModelbound 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 youstillcannot get the query correct, because it has a dependency
on something likerequest.useretc, 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 licenseIt’sFreeBSD. There’s should be aLICENSEfile 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:Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.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-storedqueries0.1.4Changed theopenandclosemethods ofTemporaryTableEditorto 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.3Raise an exception at runtime if the temporary model being passed in delaresForeignKeyetc without settingrelated_name="+"for each.Add functionality for MySQL to detect if it can use theMEMORYengine.0.1.2Initial release