Skip to content

Instantly share code, notes, and snippets.

View Greymalkin's full-sized avatar

Patricia Lee Greymalkin

View GitHub Profile
@Greymalkin
Greymalkin / gist:f892e52ec541a7220252ac31b6a2abb0
Last active April 12, 2020 14:35
Inline Admin aware of Parent Object and affecting Generic Foreign Key Content Type choices
# in models.py
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
class Screen(models.Model):
# Screen Model: Ties into all shows, drives instance selection when using generic foreign keys
name = models.CharField(max_length=50)
limit = (models.Q(
models.Q(app_label='case_story', model='casestoryshow') |
@Greymalkin
Greymalkin / gist:8068568445fcfe415f15a796e0c58a7d
Created March 17, 2017 16:18
get_request(): a way to pull request into places where it doesn't hook in naturally
# in requirements.txt
django-request-provider==1.0.2
# in settings.py
MIDDLEWARE_CLASSES += ['request_provider.middleware.RequestProvider',]
# in admin.py
from request_provider.signals import get_request
class MeetAdmin(admin.ModelAdmin):

Customize Save In Django Admin Inline Form

Background

This is a common case in django ORM.

from django.db import models

class Author(models.Model):
@Greymalkin
Greymalkin / gist:82e1359996fa7395f4e2
Last active August 29, 2015 14:26
HAHAAS: scratch pad
from django.db.models import Count
from django.db.models.sql.where import ExtraWhere, AND
from django.db import reset_queries, connection
>>> categories = '27,18'
>>> filter_string = 'ringtones_bundle_category.category_id IN ({})'.format(categories)
>>> bundle_join_string = 'ringtones_bundlepack.bundle_id = ringtones_bundle.id'
>>> category_join_string = 'ringtones_bundle_category.bundle_id = ringtones_bundle.id'
>>> models.BundlePack.objects.extra(where=[string,'ringtones_bundle_category.bundle_id = ringtones_bundle.id','ringtones_bundlepack.bundle_id = ringtones_bundle.id']).extra(tables=['ringtones_bundle_category','ringtones_bundle']).distinct()
@Greymalkin
Greymalkin / gist:d735ba6ed5cbcec556b3
Last active August 29, 2015 14:03
Bulk Rename in folder of filesystem (to lower case)
brew install rename
==> Downloading https://github.com/ap/rename
rename 'y/A-Z/a-z/' * -f
@Greymalkin
Greymalkin / gist:1990ec087ff39e4ef647
Created July 2, 2014 19:14
Loop over an objects elements
# http://stackoverflow.com/questions/11637293/iterate-over-object-attributes-in-python
def __init__(self, *args, **kwargs):
print('***** init')
for a in args:
print(a)
for k in kwargs:
print (k)
print([a for a in dir(self) if not a.startswith('__')])
print self.formset
print([a for a in dir(self.formset) if not a.startswith('__')])
@Greymalkin
Greymalkin / forms.py
Created February 26, 2014 15:55 — forked from mattoc/forms.py
"""Allows checking of form POST data as well as uploaded files when validating
a standard Django `forms.Form`.
The short version is that you check `Form().files` in the `clean()` method,
assuming the form has been bound to the request.
See:
http://mattoc.com/django-handle-form-validation-with-combined-post-and-files-data/
"""
# -*- coding: utf-8 -*-
import Image
def resize_and_crop(img_path, modified_path, size, crop_type='top'):
"""
Resize and crop an image to fit the specified size.
args:
img_path: path for the image to resize.
@Greymalkin
Greymalkin / gist:1271287
Created October 7, 2011 20:32
Step2.Application.cfc.v01
variables.framework = {};
variables.framework.defaultSection = 'main';
variables.framework.defaultItem = 'main';
variables.framework.reloadApplicationOnEveryRequest = true;
variables.framework.suppressImplicitService = true;