Skip to content

Instantly share code, notes, and snippets.


David Sanders shangxiao

View GitHub Profile
View pyproject.toml
name = "poetry-test"
version = "0.1.0"
description = ""
authors = ["Some User <>"]
python = "^3.6"
shangxiao / results
Created Jul 3, 2019
Pytest not raising import exceptions for regular classes?
View results
shangxiao ~/projects/test-pytest $ pytest
========================================================= test session starts =========================================================
platform darwin -- Python 3.6.2, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
rootdir: /Users/shangxiao/projects/test-pytest
collected 1 item F [100%]
============================================================== FAILURES ===============================================================
________________________________________________________ FooTestCase.test_foo _________________________________________________________
shangxiao /
Last active May 17, 2018
CTE's in Django
shangxiao /
Created Sep 11, 2017
FunkyBob's debug middleware
import time
from django.conf import settings
from django.db import connection
class DebugMiddleware:
def __init__(self):
import logging
self.log = logging.getLogger(__name__)
View GraphQL & Relay notes (December 2015)
Relay Notes
- Define a schema, that is used for _both_ server side endpoint setup & frontend graphql compilation
- node only for now, some graphql ports
- Relay has a strict specification of how a graphql schema is set up:
- tree children are called "plural links" (see _writePluralLink())
shangxiao /
Last active Jul 24, 2016
django faq

Frequently asked questions:

  • Why shouldn't i use mysql? / mongo?
  • Should I use jinja, I read it's faster?
  • Should I use sqlalchemy, I read it's superior?
  • How do i make this thing happen in parallel/not wait for xxx to finish?
  • Why does django ask for a default? (But my table has no data in it wtf django!?!)
    • FunkyBob | I think, perhaps, it's time I wrote a blog post on "why does a migration adding a field need a default?"
  • Why do I need to commit my migrations? Why can't I just run makemigrations on the server?
  • migrations are source, makemigrations is a helper which takes you part way
  • i've seen people accidentally lose their migrations on production…  oops now what?
shangxiao /
Created May 14, 2016
Django view decorator for returning 404 if not authenticated
from functools import update_wrapper
from django.http import Http404
class authenticated_or_404(object):
def __init__(self, func):
self.func = func
update_wrapper(self, func)
shangxiao /
Created Jul 31, 2015
Django is_authenticated gotcha
def some_view(request):
if request.user.is_authenticated:
View gist:93b983ea2313aa71137a
class ChoiceField(Field):
def to_representation(self, value):
if data == '' and self.allow_blank:
return ''
return self.choice_strings_to_values[six.text_type(value)]
except KeyError:
return value