Fix Django 1.4 admin_site ForeignKeyRawIdWidget issue

If you want use field widget from Django 1.3 in Django 1.4 you need patch it

Django Version: 1.4
Exception Type:  TypeError
Exception Value: __init__() takes at least 3 arguments (3 given)
Exception Location: in __init__, line 60
    super(Widget, self).__init__(*args, **kwargs)

The signature for ForeignKeyRawIdWidget.__init__() in Django 1.4 is:

class ForeignKeyRawIdWidget(forms.TextInput):
    def __init__(self, rel, admin_site, attrs=None, using=None):

The required argument admin_site is new to Django 1.4. The formfield method in your field doesn’t include the admin_site argument:

def formfield(self, *args, **kwargs):
    kwargs['widget'] = Widget(self.rel)
    return super(Model, self).formfield(*args, **kwargs)

This patch inspects the arguments of ForeignKeyRawIdWidget.__init__() to see if admin_site is a required argument and patches it into the __init__ kwargs for your Widget. If admin_site is required it uses as the value.

class Widget(ForeignKeyRawIdWidget):
    def __init__(self, *args, **kwargs):
        if 'admin_site' in inspect.getargspec(ForeignKeyRawIdWidget.__init__)[0]:  # Django 1.4
            kwargs['admin_site'] = site
        self.field_name = kwargs.pop('field_name')
        super(Widget, self).__init__(*args, **kwargs)

coded by nessus