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 / json
Created August 23, 2017 18:29
paintings
{
"paintings": {
"painting": [
{
"title": "Boardwalk 5",
"artist": "Arnie Palmer",
"image": "ap1.jpg",
"price": 850
},
{
@diek
diek / json
Created August 24, 2017 00:43
book titles
{
"current_page": 1,
"data": [
{
"book_ids": [
"a_century_of_spies",
"citizen_espionage",
"double_eagle_a01",
"double_eagle_a02",
"games_of_intelligence",
@diek
diek / debug_toolbar.py
Last active December 15, 2018 23:38
Django Debug Toolbar
pip install django-debug-toolbar
# Debug toolbar
Add to settings.py
def show_toolbar(request):
return True
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
@diek
diek / settings.py
Created August 5, 2020 18:41
Sample settings.py for Postgresql + Django using .env and dj-database-url
from os import getenv
from os.path import abspath, dirname, join
import dj_database_url
from dotenv import load_dotenv
load_dotenv()
DATABASE_URL = getenv("DATABASE_URL")
@diek
diek / pip-cache-install.py
Created October 28, 2020 05:30 — forked from jacobian/pip-cache-install.py
Install a package from your local pip download cache without touching the 'net.
#!/usr/bin/env python
"""
Install a package from your local pip download cache without having to touch
the 'net at all.
You'll need to be using a pip download cache; that is, you'll need the
following in your ~/.pip/pip.cfg:
[install]
@diek
diek / import_salaries.py
Created November 27, 2020 17:42
Django Management Command - Upload data from csv
import csv
from django.core.management.base import BaseCommand
from expenditures.models import Salary
class Command(BaseCommand):
help = "Insert N Rows of Salaries"
@diek
diek / models.py
Last active July 22, 2021 15:51 — forked from jacobian/models.py
An example of using many-to-many "through" to augment m2m relationships. See http://www.quora.com/How-do-you-query-with-a-condition-on-a-ManyToMany-model-in-Django for context.
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=200)
groups = models.ManyToManyField('Group', through='GroupMember', related_name='people')
class Meta:
ordering = ['name']
def __str__(self):
@diek
diek / add_search_vector.py
Created January 13, 2021 17:28
Management Command to update search_vector field
from django.contrib.postgres.search import SearchVector
from django.core.management import BaseCommand
from myapp.models import Article
class Command(BaseCommand):
# Provide help at command prompt for user
help = "Add search vector articles"
# A command must define handle()
@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">
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"])