Skip to content

Instantly share code, notes, and snippets.

@pawl
pawl / example.py
Created April 23, 2020 03:53
attempt at ModelChoiceField queryset caching
# See full example: https://github.com/pawl/django_modelchoicefield_caching/blob/master/myapp/views.py#L31-L51
for song in playlist:
form_data = {'title': song["title"], 'artist': song["artist"]}
song_form = forms.SongFormWithModelChoiceField(data=form_data)
song_form.is_valid() # runs a query to get the ModelChoiceField queryset each time
print('ModelChoiceField - query count AFTER validating all songs:',
len(connection.queries)) # 5 queries
# query for choices outside of the loop to prevent unnecessary queries
@pawl
pawl / README.md
Last active February 14, 2020 03:44
Django's biggest gotcha?

In my opinion, one of django's biggest gotchas is using RelatedManager.set with models that have non-nullable ForeignKey fields.

Example

Models.py (with a one-to-many relationship and a non-nullable ForeignKey):

class Reporter(models.Model):
    name = models.CharField(max_length=255)


class Article(models.Model):
@pawl
pawl / new_django.sh
Created January 23, 2020 03:52
starting a new django project
cd ~
mkdir mysite
virtualenv .venv -p `which python3`
source .venv/bin/activate
pip install django
django-admin startproject mysite
cd mysite
python manage.py startapp myapp
@pawl
pawl / webdav.conf
Last active August 1, 2023 05:41
Installing nginx webdav server on ubuntu 20.04
server {
listen 80;
listen [::]:80;
root /var/dav/webdav_root;
# dav allowed method
dav_methods PUT DELETE MKCOL COPY MOVE;
# Allow current scope perform specified DAV method
dav_ext_methods PROPFIND OPTIONS;
@pawl
pawl / readme.md
Last active January 8, 2023 13:39
test to see if psycogreen is necessary to run sqlalchemy + gevent + psycopg2 concurrently

This is a test to see if psycogreen is necessary to run sqlalchemy + gevent + psycopg2 concurrently.

benchmark command (concurrency of 10)

ab -n 10 -c 10 127.0.0.1:8080/wait

psychopg2 - sync worker (expecting concurrency of 2)

gunicorn -b 127.0.0.1:8080 -w 2 test:app

@pawl
pawl / pingpong.thrift
Last active March 21, 2017 02:26
demonstrates issue with thrift service swallowing TTransportException
service PingPong {
string ping(),
}
@pawl
pawl / empty_in.py
Created March 18, 2017 02:28
sqlalchemy empty list in_() issue
# demonstrates the issue fixed in: https://bitbucket.org/zzzeek/sqlalchemy/issues/3907
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
session = scoped_session(sessionmaker(autocommit=False,
@pawl
pawl / in_loading.py
Last active December 28, 2017 15:19
Example of a custom "IN()" relationship loading strategy in sqlalchemy
from collections import defaultdict
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.orm.attributes import set_committed_value
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
@pawl
pawl / subquery_nested_loading.py
Created March 15, 2017 04:07
example of subquery loading using temporary tables and filesort
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))