Skip to content

Instantly share code, notes, and snippets.

@yuekui
yuekui / disable_signals.py
Created November 4, 2020 00:59
Temporarily disable Django signals for specific receiver
```python3
from contextlib import contextmanager
from django.db.models.signals import *
@contextmanager
def disable_signals(sender, receiver, *signals):
if not signals:
signals = [pre_init, post_init, pre_save, post_save, pre_delete, post_delete]
disconnected_signals = []
@yuekui
yuekui / schema.py
Created May 21, 2021 20:59
Monkey-patch Django mysql schema to skip setting default value when altering BLOB/TEXT fields
from django.db.backends.mysql import schema
class DatabaseSchemaEditor(schema.DatabaseSchemaEditor):
@property
def _supports_limited_data_type_defaults(self):
# MariaDB >= 10.2.1 supports defaults for BLOB and TEXT.
if self.connection.mysql_is_mariadb:
return self.connection.mysql_version >= (10, 2, 1)
return False
@yuekui
yuekui / group_filter_set.py
Created March 2, 2022 21:54
GroupFilterSet to reduce inner joins for performance.
from django.db.models import Q
from django.db.models.constants import LOOKUP_SEP
from django_filters import rest_framework as django_filters
from django_filters.constants import EMPTY_VALUES
class GroupFilterSet(django_filters.FilterSet):
def filter_queryset(self, queryset):
"""
Group the fitlers by the first join table to
@yuekui
yuekui / sync_branches.yaml
Created September 21, 2022 00:31
Automatically sync branches via Github Action
name: Sync branches master -> release -> develop
on:
push:
branches:
- release
- master
jobs:
sync-release: