Skip to content

Instantly share code, notes, and snippets.

@rafales
rafales / ddl.sql
Last active October 24, 2017 11:54
Retrieving nested graphs from Postgres using JSON
BEGIN TRANSACTION;
CREATE TABLE "user"
(
"id" SERIAL PRIMARY KEY,
"name" VARCHAR NOT NULL,
"email" VARCHAR NOT NULL
);
CREATE TABLE "post"
(
"id" SERIAL PRIMARY KEY,
@rafales
rafales / recipes.py
Created June 13, 2013 14:37
Python recipies
class cached_property(object):
"""
Decorator that converts a method with a single self argument into a
property cached on the instance.
"""
def __init__(self, func):
self.func = func
def __get__(self, instance, type=None):
if instance is None:
@rafales
rafales / model_utils.py
Last active January 29, 2023 18:12
[Django] Model utils
import contextlib
from copy import deepcopy
from django.db.models.expressions import ExpressionNode, F
from django.db.models.fields.files import FileField
from django.db.models.query import QuerySet
from django.db import models, transaction
import operator
def get_object_or_none(cls, *args, **kwargs):