Skip to content

Instantly share code, notes, and snippets.

View sarajoha's full-sized avatar

Sara C. sarajoha

View GitHub Profile
@bmispelon
bmispelon / update.py
Created March 2, 2021 09:53
[Django ORM] Updating a JSONField based on the value of another field
"""
How to update JSONField based on the value of another field.
For example:
class MyModel(models.Model):
name = models.CharField(...)
data = models.JSONField()
How to update the MyModel table to store the `name` field inside the `data`
@bergpb
bergpb / convert_pdf_epub.py
Last active March 2, 2024 04:13
Script to convert pdf to epub.
import os
from time import sleep
path = os.getcwd()
files = os.listdir(path)
#install package
os.system('sudo apt install calibre -y && sudo apt update')
#remove espacos e insere _
from rest_framework import renderers
class PlainTextRenderer(renderers.BaseRenderer):
media_type = 'text/plain'
format = 'text'
def render(self, data, media_type=None, renderer_context=None):
return str(renderers.JSONRenderer().render(data, media_type, renderer_context)).encode(self.charset)
@simonw
simonw / sql_replace_in_django_orm.py
Last active April 20, 2024 18:34
How to use the SQL replace function in a Django ORM query
from django.db.models import F, Func, Value
from myapp.models import MyModel
# Annotation
MyModel.objects.filter(description__icontains='\r\n').annotate(
fixed_description=Func(
F('description'),
Value('\r\n'), Value('\n'),
function='replace',
)
@graceavery
graceavery / harryPotterAliases
Last active May 10, 2023 02:51
bash aliases for Harry Potter enthusiasts
alias accio=wget
alias avadaKedavra='rm -f'
alias imperio=sudo
alias priorIncantato='echo `history |tail -n2 |head -n1` | sed "s/[0-9]* //"'
alias stupefy='sleep 5'
alias wingardiumLeviosa=mv
alias sonorus='set -v'
alias quietus='set +v'