Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Created October 2, 2020 08:33
Show Gist options
  • Save kingbuzzman/937a9d207bd937d1b2bb22249ae6bdb2 to your computer and use it in GitHub Desktop.
Save kingbuzzman/937a9d207bd937d1b2bb22249ae6bdb2 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
from django.urls import include, path, reverse
from django.apps import apps, AppConfig
from django.http import HttpResponse
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, 'django.contrib.postgres'],
MIGRATION_MODULES={APP_LABEL: APP_MIGRATION_MODULE},
SITE_ID=1,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
},
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()
# 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.test import TestCase # noqa: E402 isort:skip
from django.db import models # noqa: E402 isort:skip
from django import forms # noqa: E402 isort:skip
from django.contrib.postgres.fields import HStoreField
from django.forms import formset_factory
from decimal import Decimal
class Product(models.Model):
name = models.CharField(max_length=255, blank=False, null=False)
class ProductVariant(models.Model):
name = models.CharField(max_length=255, blank=False, null=False)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
attributes = HStoreField()
price = models.DecimalField(blank=False, null=False, default=0, max_digits=6, decimal_places=2)
class ProductVariantForm(forms.ModelForm):
class Meta:
fields = ('name', 'product', 'attributes', 'price')
model = ProductVariant
ProductVariantFormSet = formset_factory(ProductVariantForm)
urlpatterns = []
class SimpleTestCase(TestCase):
def setUp(self):
self.product1 = Product.objects.create(name='Circle')
self.product2 = Product.objects.create(name='Square')
self.product3 = Product.objects.create(name='Triangle')
def test_form(self):
form = ProductVariantForm(data={'name': 'thing', 'product': self.product1.id, 'attributes': {'a': 1, 'b': 2}, 'price': '100.00'})
self.assertEqual(True, form.is_valid())
instance = form.save()
self.assertEqual('thing', instance.name)
self.assertEqual('Circle', instance.product.name)
self.assertEqual({'a': '1', 'b': '2'}, instance.attributes)
self.assertEqual(Decimal('100.00'), instance.price)
def test_formset(self):
data = {
'form-INITIAL_FORMS': '1',
'form-TOTAL_FORMS': '3',
'form-MAX_NUM_FORMS': '1000',
'form-0-name': 'thing',
'form-0-product': self.product1.id,
'form-0-attributes': {'a': 1, 'b': 1},
'form-0-price': '100.00',
'form-1-name': 'thing2',
'form-1-product': self.product2.id,
'form-1-attributes': {'a': 2, 'b': 2},
'form-1-price': '200.00',
'form-2-name': 'thing3',
'form-2-product': self.product3.id,
'form-2-attributes': {'a': 3, 'b': 3},
'form-2-price': '300.00'
}
formset = ProductVariantFormSet(data=data)
self.assertEqual(True, formset.is_valid())
instances = []
for form in formset:
if form.is_valid():
instances.append(form.save())
self.assertEqual('thing', instances[0].name)
self.assertEqual('Circle', instances[0].product.name)
self.assertEqual({'a': '1', 'b': '1'}, instances[0].attributes)
self.assertEqual(Decimal('100.00'), instances[0].price)
self.assertEqual('thing2', instances[1].name)
self.assertEqual('Square', instances[1].product.name)
self.assertEqual({'a': '2', 'b': '2'}, instances[1].attributes)
self.assertEqual(Decimal('200.00'), instances[1].price)
self.assertEqual('thing3', instances[2].name)
self.assertEqual('Triangle', instances[2].product.name)
self.assertEqual({'a': '3', 'b': '3'}, instances[2].attributes)
self.assertEqual(Decimal('300.00'), instances[2].price)
# 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

docker run -it --rm python:3.7 bash -xc '
apt update && 
apt install postgresql -y && 
su - postgres -c "/usr/lib/postgresql/11/bin/initdb -D /tmp/db"
su - postgres -c "/usr/lib/postgresql/11/bin/postgres -D /tmp/db" &

curl -L https://gist.githubusercontent.com/kingbuzzman/937a9d207bd937d1b2bb22249ae6bdb2/raw/formset_example.py > formset_example.py
pip install django==2.2 psycopg2==2.8.6
psql -U postgres -d template1 -c "CREATE EXTENSION IF NOT EXISTS hstore;"
python formset_example.py makemigrations
python formset_example.py test
'

Answers stackoverflow question: https://stackoverflow.com/questions/64167862/how-to-create-multiple-objects-with-different-values-at-a-time-in-django

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