stefanfoulis/django-filer — pull request #175 by tomscytale (2012-03-02 11:42:27+00:00)
Title: fix Django1.4 compatibility issue

Django1.4b1

File "filer/fields/file.py" in __init__
103. forms.Field.__init__(self, widget=self.widget(rel), *args, **kwargs)

Exception Type: TypeError at /
Exception Value: Error when calling the metaclass bases
__init__() takes at least 3 arguments (2 given)

the relevant lines in `file.py` (`folder.py` has the same issue) are:

class AdminFileFormField(forms.ModelChoiceField):
widget = AdminFileWidget
def __init__(self, rel, queryset, to_field_name, *args, **kwargs):
forms.Field.__init__(self, widget=self.widget(rel), *args, **kwargs)

the exception is triggered by the call `self.widget(rel)`

`self.widget` is `AdminFileWidget` (the class itself, not an instance) which is a subclass of `django.contrib.admin.widgets.ForeignKeyRawIdWidget`

The signature for `ForeignKeyRawIdWidget.__init__()` is:

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

the required argument `admin_site` is new in Django 1.4. So the call `self.widget(rel)` should actually be `self.widget(rel, admin_site)` where `admin_site` is an instance of `django.contrib.admin.sites.AdminSite`

This patch inspects the arguments of `ForeignKeyRawIdWidget.__init__()` to see if `admin_site` is a required argument to determine appropriate arguments to use.
If `admin_site` is required is uses `django.contrib.admin.sites.site` as the value. This may cause issues in cases where multiple admin sites are defined but I am not sure how django-filer currently deals with this.