Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@cb109
cb109 / django_migrate_m2m_add_through_model.py
Last active June 13, 2024 21:43
Fix: ValueError: Cannot alter field <model>.<field> into <model>.<field> - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
# Update: Turns out there is a specific tool for this kind of migration that should
# do a better job as my multistep-migration below, check out SeparateDatabaseAndState:
#
# https://docs.djangoproject.com/en/5.0/howto/writing-migrations/#changing-a-manytomanyfield-to-use-a-through-model
# Changing the through= part of a models.ManyToManyField() fails to migrate,
# as Django won't let us do this in a single step. We can manually workaround
# with several migration steps though, as shown below.
#
# Based on Django 3.2
@cb109
cb109 / gist:4039770a33ead38fb77789c107dd080f
Created September 1, 2023 10:53
Django FileField / File .path VS .name
# Django's models.FileField stores FieldFile objects. These have both .name
# and .path attributes, which however are not exactly intuitive:
my_instance.my_filefield.name
# E.g. whatever/123/foo.png
# Returns the path relative to MEDIA_ROOT, not the filename!
# For the filename do e.g. os.path.basename(my_instance.my_filefield.name)
my_instance.my_filefield.path
# E.g. /var/www/media/whatever/123/foo.png
@cb109
cb109 / mprof.sh
Created August 9, 2023 07:36
Plot memory usage of any python process
# https://pypi.org/project/memory-profiler/
pip install -U memory_profiler
mprof run --include-children --attach <pid>
mprof plot
@cb109
cb109 / fix_num_db_queries_from_test_output.py
Last active July 14, 2023 10:48
rewrite contextmanager arguments based on pytest output using redbaron
"""A script to help update many querycount assertions in code quickly.
Requirements:
pip install redbaron
Deprecation note:
redbaron (based on baron) is somewhat unmaintained and only supports
Python grammar u to version 3.7, so for newer Python versions we may
@cb109
cb109 / closeVuetifyDialog.js
Created June 1, 2023 13:12
Close opened Vuetify Dialog from outside any Vue app (Vuetify 1.5.x)
document.querySelector('.v-dialog--active').__vue__.$options._renderChildren[0].context.isActive = false
@cb109
cb109 / 1_dropzone.html
Last active January 10, 2023 13:12
Integrate Dropzone UI with jquery AJAX POST and django-filer FilerImageField
<head>
<link rel="stylesheet" href="/static/css/dropzone.css">
<script type="text/javascript" src="/static/js/dropzone.js"></script>
</head>
<form
id="my-dropzone-form"
class="dropzone"
action="/cannot-be-empty-but-is-not-used-instead-see-submit.js"
></form>
@cb109
cb109 / django_admin_autocomplete_field_label_as_link.js
Last active January 6, 2023 13:33
Turn Django admin autocomplete_field labels into links to the chosen model instance
@cb109
cb109 / setup_graylog_server.md
Last active March 6, 2026 09:49
Setup a self-hosted Graylog server

Setup a self-hosted Graylog server (e.g. for Django applications)

So other machines can send their logs to the central server for inspection/debugging.

For ease of setup we are going to use the official docker-compose setup here. The example below will be using Debian 11.

  1. Get a VPS with at least 4GB of RAM (less may crash as opensearch demands some resources).
  2. ssh to that VPS.
  3. Install Docker:
@cb109
cb109 / size_of_databases.sql
Created October 10, 2022 10:52
MySQL Size of Databases and Tables
SELECT
table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (GB)"
FROM
information_schema.TABLES
GROUP BY
table_schema;
@cb109
cb109 / track_data.py
Last active July 25, 2022 08:08 — forked from dcramer/track_data.py
Tracking changes on properties in Django
# Tested against Django 3.2.14.
from django.db.models.base import DEFERRED
def track_data(*fields):
"""Tracks property changes on a model instance.
Simplified version of David Cramer's gist:
https://gist.github.com/dcramer/730765