diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 50e55dc4d5..32472cc7d8 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -105,13 +105,12 @@ class ForeignKeyRawIdWidget(forms.TextInput): super(ForeignKeyRawIdWidget, self).__init__(attrs) def render(self, name, value, attrs=None): - from django.contrib.admin.views.main import TO_FIELD_VAR related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower()) - params = {} - if self.rel.limit_choices_to: - params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()])) - params.update({TO_FIELD_VAR: self.rel.get_related_field().name}) - url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()]) + params = self.url_parameters() + if params: + url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()]) + else: + url = '' if not attrs.has_key('class'): attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook. output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)] @@ -123,7 +122,19 @@ class ForeignKeyRawIdWidget(forms.TextInput): if value: output.append(self.label_for_value(value)) return mark_safe(u''.join(output)) - + + def base_url_parameters(self): + params = {} + if self.rel.limit_choices_to: + params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()])) + return params + + def url_parameters(self): + from django.contrib.admin.views.main import TO_FIELD_VAR + params = self.base_url_parameters() + params.update({TO_FIELD_VAR: self.rel.get_related_field().name}) + return params + def label_for_value(self, value): key = self.rel.get_related_field().name obj = self.rel.to.objects.get(**{key: value}) @@ -144,7 +155,10 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): else: value = '' return super(ManyToManyRawIdWidget, self).render(name, value, attrs) - + + def url_parameters(self): + return self.base_url_parameters() + def label_for_value(self, value): return '' diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py index 2bc907557a..19e2696b93 100644 --- a/tests/regressiontests/admin_widgets/models.py +++ b/tests/regressiontests/admin_widgets/models.py @@ -69,7 +69,7 @@ Currently: alb >>> rel = Album._meta.get_field('band').rel >>> w = ForeignKeyRawIdWidget(rel) >>> print conditional_escape(w.render('test', band.pk, attrs={})) - Lookup Linkin Park + Lookup Linkin Park >>> m1 = Member.objects.create(pk=1, name='Chester') >>> m2 = Member.objects.create(pk=2, name='Mike') @@ -100,7 +100,7 @@ True >>> rel = Inventory._meta.get_field('parent').rel >>> w = ForeignKeyRawIdWidget(rel) >>> print w.render('test', core.parent_id, attrs={}) - Lookup Apple + Lookup Apple """ % { 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 'STORAGE_URL': default_storage.url(''),