Skip to content

Instantly share code, notes, and snippets.

@jamesbrobb
jamesbrobb / linkify.py
Created October 4, 2023 15:18
Update of the linkify function for django admin that also handles ManyToManyField
from typing import Optional
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.db.models import ManyToManyField, ForeignKey, Model
from django.urls import reverse
from django.utils.html import format_html
def create_link(linked_obj, app_label: str, label_prop: Optional[str] = None) -> str:
@jamesbrobb
jamesbrobb / ts_cheatsheet.ts
Last active June 4, 2023 13:52
Typescript generics cheatsheet
/*
Array/Tuple access
type T = ['Item One', 'Item Two', 'Item Three', 'Item Four']
type Util<T extends readonly unknown[]> = % example %
*/
T['length'] // 4
@jamesbrobb
jamesbrobb / __init__.py
Last active November 6, 2020 15:59
Overriding Django's {% extends %} template tag to prevent a parent template rendering in a third party app
"""
This needs to go somewhere that will force it to run immediately.
As the 'extends' tag always has to be first in a template, it's not
possible to use the {% load %} tag for this. Instead it must be
loaded in the same way as the django tags.
add_to_builtins function changed module in 1.7
"""
try:
@jamesbrobb
jamesbrobb / multiform.py
Last active July 4, 2023 18:53 — forked from michelts/gist:1029336
django multiform mixin and view that allows the submission of a) All forms b) Grouped forms c) An individual form
class MultiFormMixin(ContextMixin):
form_classes = {}
prefixes = {}
success_urls = {}
grouped_forms = {}
initial = {}
prefix = None
success_url = None