Skip to content

Instantly share code, notes, and snippets.

View ihadgraft's full-sized avatar

ihadgraft

  • Duke University
  • Durham, NC
View GitHub Profile
@ihadgraft
ihadgraft / admin.py
Created October 20, 2021 11:35
Setting the initial value for a field on an inline with Django admin
"""
This examples shows setting the current user as the default value for a field named 'user'
"""
class MyInline(admin.TabularInline):
def formfield_for_dbfield(self, db_field, request, **kwargs):
field = super().formfield_for_dbfield(db_field, request, **kwargs)
if db_field.name == 'user':
field.initial = request.user
return field
@ihadgraft
ihadgraft / django_instance_to_inline.md
Created October 20, 2021 11:03
Strategy for passing a model instance from a ModelAdmin to an Inline

This demonstrates creating a mixin for inlines along with a custom ModelAdmin to inject the instance into the inline for further processing.

One use case is for when you need to override get_field_queryset to filter available options in a dropdown field to limit selection to only those items associated with the parent.

class InstanceAwareInlineMixin(object):
    """
    Create a mixin that supports setting an instance.
    """
    def __init__(self, *args, **kwargs):
python scrapy-simple-processor.py https://example.com --output-file=/path/to/file.csv
@ihadgraft
ihadgraft / 00-README.md
Last active June 25, 2018 13:27
DNS queries with Python

DNS queries with Python

These examples use DNSPython

pip install dnspython
@ihadgraft
ihadgraft / docker-compose.yml
Last active May 8, 2017 22:19
Getting started with docker-compose and the dukewebservices images
version: "2"
services:
data:
image: busybox:latest
volumes:
- ./persistent/html:/var/www/html:cached
- ./persistent/mail:/var/mail:cached
- ./persistent/sql-data:/var/lib/mysql
- ./persistent/backup:/var/backup
- ./persistent/logs/apache:/var/apache2/logs