Skip to content

Instantly share code, notes, and snippets.

Avatar

Derrick Kearney diek

  • Halifax, NS, Canada
View GitHub Profile
View upload_csv.py
# 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 Apr 12, 2022 — forked from bradtraversy/django_deploy.md
Django Deployment - Digital Ocean
View django_deploy.md

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 Feb 4, 2022 — forked from bmispelon/placeholderify.py
Automatic placeholder attributes from field labels in Django forms
View placeholderify.py
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 Nov 16, 2021 — forked from mariocesar/admin.py
Django admin decorator to create a confirmation form action, like the default delete action works
View admin.py
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 Sep 9, 2021 — forked from rg3915/gen_random_datetime.py
Generate Random Datetime Python
View gen_random_datetime.py
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)
View add_users.py
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 — forked from Wombatpm/cbv_multiple_forms.html
Django multiple forms code with sample usage
View cbv_multiple_forms.html
{% 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">
@diek
diek / add_search_vector.py
Created Jan 13, 2021
Management Command to update search_vector field
View add_search_vector.py
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 / models.py
Last active Jul 22, 2021 — 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.
View models.py
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 / import_salaries.py
Created Nov 27, 2020
Django Management Command - Upload data from csv
View import_salaries.py
import csv
from django.core.management.base import BaseCommand
from expenditures.models import Salary
class Command(BaseCommand):
help = "Insert N Rows of Salaries"