Skip to content

Instantly share code, notes, and snippets.

View egasimus's full-sized avatar
🐒
all our telepaths are on leave and the ascended master is currently downshifting

egasimus

🐒
all our telepaths are on leave and the ascended master is currently downshifting
View GitHub Profile
@egasimus
egasimus / gist:8352602
Created January 10, 2014 14:02
Simple M2M filter for Django admin.
class M2MFilter(admin.SimpleListFilter):
title = _('yourm2mfield')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'yourm2mfield'
def lookups(self, request, model_admin):
return ((x.pk, x.title) for x in YourRelatedModel.objects.all())
def value(self):
@egasimus
egasimus / Readme.md
Last active January 5, 2021 08:55
A few remarks on the Django REST Framework

A few remarks on the Django REST Framework

For a good while, I've been interested in the topic of automatically generating and routing views. For my last project, I built a haphazard implementation which ran using an elaborate scheme of mixins and decorators. A veritable monstrosity, it was - and the cognitive load of working with it wasted over a month of my time, allowing the project to slowly descend into limbo. I eventually ended up re-writing the heaps of class-based views by hand. Even though making good use of inheritance helped me tremendously, it was still a chore, and my interest in a tool that would save me some boilerplate code for standard CRUD apps remained.

When I first got a glimpse of the Django REST Framework, I was quite impressed by the browsable API; a quick look at the documentation got me hooked onto the concept of ViewSets and Routers. A few days ago, I decided to finally get my feet wet with the DRF and use it in my latest project. I honestly believed that it would b

@egasimus
egasimus / cbv_factory.py
Last active October 25, 2020 00:00
Django CBV factory function, with example usage.
from inspect import isfunction
from django.views.generic import View, CreateView, UpdateView, DeleteView, DetailView, ListView
from django.views.generic.edit import ModelFormMixin
from django.views.generic.detail import SingleObjectMixin
def parse_func_dict(selfobj, d):
"""
Recursively goes through a dict, calling functions that refer to a view's
self and therefore need to be called from within -- such as getting extra
kwargs for a form constructor based on request data.