Skip to content

Instantly share code, notes, and snippets.

View diek's full-sized avatar

Derrick Kearney diek

  • Halifax, NS, Canada
View GitHub Profile
@diek
diek / django-postgresql-15.md
Created June 27, 2024 18:34 — forked from axelbdt/django-postgresql-15.md
# Django and PostgreSQL 15, the rules have changed

Django and PostgreSQL 15, the rules have changed

Postgres 15 is just out, and while there is a lot to love about this new release, you're in for a surprise if you try to set it up with Django following tutorials like this one.

The reason is stated in the release announcement:

Remove PUBLIC creation permission on the public schema (Noah Misch) The new default is one of the secure schema usage patterns that Section 5.9.6 has recommended...

Provided your web app doesn't access your database as a superuser (it shouldn't) and uses a dedicated user, it is not allowed to use the public schema anymore. You have to create one for this specific user, and the next section will

from django.db import models
from django.utils import timezone
""" Source: https://twitter.com/JackDLinke/status/1578435998492475392/photo/1"""
class ItemQuerySet(self):
"""Used to return a QuerySet that has been filtered"""
def expired_items(self):
return self.filter(expired_isnull=True)
@diek
diek / books.csv
Created September 14, 2022 21:49 — forked from jaidevd/books.csv
Title Author Genre Height Publisher
Fundamentals of Wavelets Goswami, Jaideva signal_processing 228 Wiley
Data Smart Foreman, John data_science 235 Wiley
God Created the Integers Hawking, Stephen mathematics 197 Penguin
Superfreakonomics Dubner, Stephen economics 179 HarperCollins
Orientalism Said, Edward history 197 Penguin
Nature of Statistical Learning Theory, The Vapnik, Vladimir data_science 230 Springer
Integration of the Indian States Menon, V P history 217 Orient Blackswan
Drunkard's Walk, The Mlodinow, Leonard science 197 Penguin
Image Processing & Mathematical Morphology Shih, Frank signal_processing 241 CRC
# This file must be saved in app/management/commands/upload_csv.py
# https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/
from chinook.models import Playlist, PlaylistTrack, Track
from django.core.management import BaseCommand
def get_playlisttrack():
data = []
with open('PlaylistTrack.csv') as infile:
next(infile)
@diek
diek / django_deploy.md
Created April 12, 2022 14:09 — forked from bradtraversy/django_deploy.md
Django Deployment - Digital Ocean

Django Deployment to Ubuntu 18.04

In this guide I will go through all the steps to create a VPS, secure it and deploy a Django application. This is a summarized document from this digital ocean doc

Any commands with "$" at the beginning run on your local machine and any "#" run when logged into the server

Create A Digital Ocean Droplet

Use this link and get $10 free. Just select the $5 plan unless this a production app.

@diek
diek / placeholderify.py
Created February 4, 2022 05:16 — forked from bmispelon/placeholderify.py
Automatic placeholder attributes from field labels in Django forms
from functools import partial
def placeholderify(form=None, fields=None):
"""
A decorator for Django forms that sets a `placeholder` attribute to all
fields. Each field's label is used as a placeholder.
Use it like so:
@diek
diek / admin.py
Created November 16, 2021 19:21 — forked from mariocesar/admin.py
Django admin decorator to create a confirmation form action, like the default delete action works
from .models import Post, Category
from .decorators import action_form
class PostCategoryForm(forms.Form):
title = 'Update category for the selected posts'
category = forms.ModelChoiceField(queryset=Category.objects.all())
@admin.register(Post)
@diek
diek / gen_random_datetime.py
Created September 9, 2021 21:23 — forked from rg3915/gen_random_datetime.py
Generate Random Datetime Python
import random
from datetime import datetime, timedelta
min_year=1900
max_year=datetime.now().year
start = datetime(min_year, 1, 1, 00, 00, 00)
years = max_year - min_year+1
end = start + timedelta(days=365 * years)
from datetime import datetime
from random import randint
import pytz
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone
from faker import Faker
fake = Faker(["en_CA"])
@diek
diek / cbv_multiple_forms.html
Created May 25, 2021 17:47 — forked from Wombatpm/cbv_multiple_forms.html
Django multiple forms code with sample usage
{% extends "base.html" %}
{% block content %}
<form method="post">{% csrf_token %}
{{ forms.subscription }}
<input type="submit" value="Subscribe">
</form>
<form method="post">{% csrf_token %}
{{ forms.contact }}
<input type="submit" value="Send">