Skip to content

Instantly share code, notes, and snippets.

View pyshawon's full-sized avatar
🏠
Working from home

Shaid Hasan Shawon pyshawon

🏠
Working from home
View GitHub Profile
@pyshawon
pyshawon / iterm2-solarized.md
Created June 30, 2024 16:12 — forked from kevin-smets/iterm2-solarized.md
iTerm2 + Oh My Zsh + Solarized color scheme + Source Code Pro Powerline + Font Awesome + [Powerlevel10k] - (macOS)

Default

Default

Powerlevel10k

Powerlevel10k

@pyshawon
pyshawon / conditional_orderby.py
Created December 8, 2022 06:47 — forked from jperelli/conditional_orderby.py
Conditional order by in django's ORM
"""
Some table has two Date fields: expiredate and SALexpiredate
Both fields can be null
when SALexpiredate is not null, overrides expiredate
when ordering:
if SALexpiredate is not null, that field needs to be used
otherwise fallback to use expiredate
"""
from django.db.models import DateField, Case, When, F
@pyshawon
pyshawon / gist:5db1dea89e49031d86d454f944a1de73
Created October 4, 2021 03:44 — forked from ryanpitts/gist:1304725
GROUP BY and Select MAX from each group in Django, 2 queries
'''
given a Model with:
category = models.CharField(max_length=32, choices=CATEGORY_CHOICES)
pubdate = models.DateTimeField(default=datetime.now)
<other fields>
Fetch the item from each category with the latest pubdate.
'''
@pyshawon
pyshawon / gist:4f39c6b32bbd17be72ee3632affe99df
Created July 15, 2020 18:18 — forked from bsnux/gist:4672788
Storing Django queryset in session. Useful when you need to pass querysets between requests
import pickle
# Session key
key = 'my_qs'
# Pizza => model example
qs = Pizza.objects.filter(ingredient='tomato')
# Dumping data
request.session[key] = pickle.dumps(qs.query)
@pyshawon
pyshawon / django_field_update_checker.txt
Created January 19, 2020 17:24 — forked from alican/django_field_update_checker.txt
check if django model fields changed after save
def DjangoModel(models.Model):
@classmethod
def from_db(cls, db, field_names, values):
instance = super().from_db(db, field_names, values)
instance._state.adding = False
instance._state.db = db
instance._old_values = dict(zip(field_names, values))
return instance