Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Last active September 17, 2019 21:46
Show Gist options
  • Save kingbuzzman/fd2b635f2cf011f1330a6be088ce3664 to your computer and use it in GitHub Desktop.
Save kingbuzzman/fd2b635f2cf011f1330a6be088ce3664 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Stolen from: https://mlvin.xyz/django-single-file-project.html
import datetime
import inspect
import os
import sys
from types import ModuleType
import django
from django.conf import settings
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# The current name of the file, which will be the name of our app
APP_LABEL, _ = os.path.splitext(os.path.basename(os.path.abspath(__file__)))
# Migrations folder need to be created, and django needs to be told where it is
APP_MIGRATION_MODULE = '%s_migrations' % APP_LABEL
APP_MIGRATION_PATH = os.path.join(BASE_DIR, APP_MIGRATION_MODULE)
# Create the folder and a __init__.py if they don't exist
if not os.path.exists(APP_MIGRATION_PATH):
os.makedirs(APP_MIGRATION_PATH)
open(os.path.join(APP_MIGRATION_PATH, '__init__.py'), 'w').close()
# Hack to trick Django into thinking this file is actually a package
sys.modules[APP_LABEL] = sys.modules[__name__]
sys.modules[APP_LABEL].__path__ = [os.path.abspath(__file__)]
settings.configure(
DEBUG=True,
ROOT_URLCONF='%s.urls' % APP_LABEL,
MIDDLEWARE=(),
INSTALLED_APPS=[APP_LABEL],
MIGRATION_MODULES={APP_LABEL: APP_MIGRATION_MODULE},
SITE_ID=1,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
},
LOGGING={
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': "%(levelname)s %(message)s",
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
}
},
'loggers': {
'django.db.backends': {'handlers': ['console'], 'level': 'DEBUG', 'propagate': False},
'django.db.backends.schema': {'level': 'ERROR'}, # Causes sql logs to duplicate -- really annoying
}
},
STATIC_URL='/static/'
)
django.setup()
from django.apps import apps # noqa: E402 isort:skip
# Setup the AppConfig so we don't have to add the app_label to all our models
def get_containing_app_config(module):
if module == '__main__':
return apps.get_app_config(APP_LABEL)
return apps._get_containing_app_config(module)
apps._get_containing_app_config = apps.get_containing_app_config
apps.get_containing_app_config = get_containing_app_config
# Your code below this line
# ##############################################################################
from django.db import models # noqa: E402 isort:skip
from django.test import TestCase # noqa: E402 isort:skip
class ModelA(models.Model):
somecriteria = models.CharField(max_length=50)
class ModelB(models.Model):
somerelation = models.ForeignKey(ModelA, on_delete=models.CASCADE)
someattribute = models.CharField(max_length=50)
urlpatterns = []
class SimpleTestCase(TestCase):
def setUp(self):
one = ModelA.objects.create(somecriteria='1')
ModelB.objects.create(somerelation=one, someattribute='1')
ModelB.objects.create(somerelation=one, someattribute='2')
ModelB.objects.create(somerelation=one, someattribute='1')
two = ModelA.objects.create(somecriteria='2')
ModelB.objects.create(somerelation=two, someattribute='2')
ModelB.objects.create(somerelation=two, someattribute='1')
three = ModelA.objects.create(somecriteria='3')
# Note this doesn't have any ModelB intensionally
def test_solution(self):
from django.db import models # needs to be here becuase of the hack that is having a single django file # noqa
expected = [('1', ['1', '1']), ('2', ['2']), ('3', [])]
actual = []
with self.assertNumQueries(2):
modelb_qs = models.Subquery(ModelB.objects.filter(someattribute=models.OuterRef('somerelation__somecriteria')).values('id'))
qs = ModelA.objects.prefetch_related(models.Prefetch('modelb_set', queryset=ModelB.objects.filter(id__in=modelb_qs)))
for obj in qs:
temp = (obj.somecriteria, [])
if len(obj.modelb_set.all()):
temp[1].extend([x.someattribute for x in obj.modelb_set.all()])
actual.append(temp)
self.assertEqual(actual, expected)
# Your code above this line
# ##############################################################################
# Used so you can do 'from <name of file>.models import *'
models_module = ModuleType('%s.models' % (APP_LABEL))
tests_module = ModuleType('%s.tests' % (APP_LABEL))
urls_module = ModuleType('%s.urls' % (APP_LABEL))
urls_module.urlpatterns = urlpatterns
for variable_name, value in list(locals().items()):
# We are only interested in models
if inspect.isclass(value) and issubclass(value, models.Model):
setattr(models_module, variable_name, value)
# We are only interested in tests
if inspect.isclass(value) and issubclass(value, TestCase):
setattr(tests_module, variable_name, value)
# Setup the fake modules
sys.modules[models_module.__name__] = models_module
sys.modules[tests_module.__name__] = tests_module
sys.modules[urls_module.__name__] = urls_module
sys.modules[APP_LABEL].models = models_module
sys.modules[APP_LABEL].tests = tests_module
sys.modules[APP_LABEL].urls = urls_module
if __name__ == "__main__":
# Hack to fix tests
argv = [arg for arg in sys.argv if not arg.startswith('-')]
if len(argv) == 2 and argv[1] == 'test':
sys.argv.append(APP_LABEL)
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
else:
from django.core.wsgi import get_wsgi_application
get_wsgi_application()
@kingbuzzman
Copy link
Author

In order to run the code above run the following:

curl -L https://gist.githubusercontent.com/kingbuzzman/fd2b635f2cf011f1330a6be088ce3664/raw/modelabquery.py > modelabquery.py
pip install django==2.1 
python modelabquery.py makemigrations
python modelabquery.py test

If you want the REAL copy and paste version:

docker run -it --rm python:3.7 bash -c '
curl -L https://gist.githubusercontent.com/kingbuzzman/fd2b635f2cf011f1330a6be088ce3664/raw/modelabquery.py > modelabquery.py
pip install django==2.1 
python modelabquery.py makemigrations
python modelabquery.py test
'

Answers stackoverflow question: https://stackoverflow.com/questions/57461476/prefetch-related-with-a-filter-that-contains-a-field-of-the-original-model/57463113#57463113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment