Skip to content

Instantly share code, notes, and snippets.

Nested ViewSets for DRF

DRF doesn't allow nested ViewSets natively, so I figured out the least amount of tweaks required to get it working.

The fields that are provided allow you to override the reverse() call with a specific mapping. These cannot be used across urls yet, as it pulls straight from the kwargs.

In your field, you must define map, which is a dictionary of destinaton => previous kwargs keys. These keys are relative to the current URL.

@kevin-brown
kevin-brown / select2.md
Last active June 26, 2016 16:49
Websites and applications which use Select2. This is not official or anything.
@kevin-brown
kevin-brown / views.py
Created May 24, 2014 14:33
Fixed DisablePaginationMixin. This needs to be added to your views.
class DisablePaginationMixin(object):
def get_paginate_by(self, queryset=None):
if self.paginate_by_param in self.request.QUERY_PARAMS and \
self.request.QUERY_PARAMS[self.paginate_by_param] == '0':
return None
return super(DisablePaginationMixin, self).get_paginate_by(queryset)
@kevin-brown
kevin-brown / mixins.py
Last active November 28, 2019 09:27
DRF and Django compatible initialization mixin
class CompatibleDispatchInitialization(object):
"""
Allow data to be initialized before a view is fully dispatched in a way
that is consistent across both standard Django views and Django REST
Framework views.
While initializing data would typically be done within `dispatch`, the
request is not fully initialized at that point during DRF views which
causes issues with authentication. For this reason, it is recommended to
do data initialization within the `initial` method for DRF views. The
# Do not include the browsable api renderer in the initial list of classes
if not DEBUG:
REST_FRAMEWORK["DEFAULT_RENDERER_CLASSES"] += [
"rest_framework.renderers.BrowsableAPIRenderer",
]
@kevin-brown
kevin-brown / convert.py
Last active December 7, 2017 01:50
MySQL to SQLite database dump converter
import fileinput
import re
import sys
file_name = sys.argv[1]
new_file = sys.argv[2]
rh = open(file_name, "r")
lines = rh.readlines()
rh.close()
@kevin-brown
kevin-brown / workflow.md
Last active August 29, 2015 14:06
MouseTrap development workflow

Development workflow

Over the summer we switched our workflow to include GitHub in order to help up facilitate the rewrite of MouseTrap. Now that the summer is over, we are left wondering how we should handle contributions to the project.

MouseTrap workflows

MouseTrap has had two main workflows during the time that I have been on the project.

@kevin-brown
kevin-brown / developing.md
Created September 6, 2014 21:07
Installing MouseTrap on Windows

Assuming bare, untouched Windows 7 Ultimate x64

Install git

http://git-scm.com/downloads

Clone repository

git clone https://github.com/GNOME-MouseTrap/mousetrap.git
@kevin-brown
kevin-brown / python-3.md
Last active August 29, 2015 14:07
MouseTrap instructions
  1. Install Python 3 and the development packages
  • sudo yum install python3 python3-devel
  1. Install the package manager for Python 3
  • sudo yum install python3-setuptools python3-pip
  1. Modify setup.py to work for us.
  • Change os.walk(LOCALE_PATH).next() to next(os.walk(LOCALE_PATH)) on line 123
  1. Install MouseTrap using pip
@kevin-brown
kevin-brown / backtrace.txt
Last active August 29, 2015 14:08
MouseTrap Backtrace
#0 0x00007fffe1c8230e in int cv::predictOrderedStump<cv::HaarEvaluator>(cv::CascadeClassifierImpl&, cv::Ptr<cv::FeatureEvaluator>&, double&) () from /usr/local/lib/libopencv_objdetect.so.3.0
#1 0x00007fffe1c82e67 in cv::CascadeClassifierImpl::runAt(cv::Ptr<cv::FeatureEvaluator>&, cv::Point_<int>, int, double&) () from /usr/local/lib/libopencv_objdetect.so.3.0
#2 0x00007fffe1c85639 in cv::CascadeClassifierInvoker::operator()(cv::Range const&) const () from /usr/local/lib/libopencv_objdetect.so.3.0
#3 0x00007fffe1c863d1 in cv::CascadeClassifierImpl::detectMultiScaleNoGrouping(cv::_InputArray const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, cv::Size_<int>, cv::Size_<int>, bool) () from /usr/local/lib/libopencv_objdetect.so.3.0
#4 0x00007fffe1c8e42d in cv::CascadeClassifierImpl::detectMultiScale(cv::_InputArray const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, std::vect