Django RelatedFieldWidgetWrapper

The Django admin interface represents an Foreign Key field, as a select with a + button. This allows you to select an existing item from the foreign table, or create a new one with a pop up window. Very useful.

I wanted to add an edit button, so that you could select one of the items and click a button to edit that item in a pop up. I initially looked at the widget that displays the <select> which is django.forms.widgets.Select. However this literally only renders the <select>, not the + button.  So I then grepped the whole of the django source to find the code that renders the button. It is django.contrib.admin.widgets.RelatedFieldWidgetWrapper. So I wrote my own subclass of RelatedFieldWidgetWrapper and looked for the place to specify it. ModelAdmin.formfield_overrides was the most most obvious place. The catch is that, that requires a widget, my custom sublcass is not a widget, its a widget wrapper. RelatedFieldWidgetWrapper is only used once, and is hard coded in django.contrib.admin.options, so I can’t use my sub class without making changes to core :-(

Next option is to add the link via javascript in an admin template override.

 

Result:

Django RemoteUserBackend for REMOTE_USER authentication and LDAP authorisation.

This class was written to integrate a django app with our LDAP/Kerberos setup. Apache handles Kerberos and passes the name of an authorised user via the REMOTE_USER variable. This script does a simple LDAP lookup to determine if the user should be a superuser. You could easily modify this to add users to different groups depending on which LDAP groups they are in.

Django Middleware Order

I got stung by this today. I was presented with the following error:

The Django remote user auth middleware requires the authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert ‘django.contrib.auth.middleware.AuthenticationMiddleware’ before the RemoteUserMiddleware class.

Even though my MIDDLEWARE_CLASSES complied with the request above:

MIDDLEWARE_CLASSES = (

‘django.middleware.common.CommonMiddleware’, ‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.auth.middleware.RemoteUserMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’,

)

Re-Ordered to:

MIDDLEWARE_CLASSES = ( ‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.middleware.common.CommonMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.RemoteUserMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, )

And the error went away :-/

Creating a debian package from a python distutils package

This is the process for converting a python package into a debian package, this is helpful when runnign a debian system, as you can then manage python modules with standard debian package management tools.

  1. Install python-stdebpython and debian logos
  2. Convert python package to debain source package: “py2dsc python-module.zip”
  3. This creates a deb_dist dir containg a sub dir with the same name as the original zip, cd to it.
  4. Build binary package from debian source package: “dpkg-buildpackage”
  5. The binary package will be in the parent directory.