diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
index 50e55dc..6a14b66 100644
a
|
b
|
class ForeignKeyRawIdWidget(forms.TextInput):
|
105 | 105 | super(ForeignKeyRawIdWidget, self).__init__(attrs) |
106 | 106 | |
107 | 107 | def render(self, name, value, attrs=None): |
108 | | from django.contrib.admin.views.main import TO_FIELD_VAR |
109 | 108 | related_url = '../../../%s/%s/' % (self.rel.to._meta.app_label, self.rel.to._meta.object_name.lower()) |
110 | | params = {} |
111 | | if self.rel.limit_choices_to: |
112 | | params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()])) |
113 | | params.update({TO_FIELD_VAR: self.rel.get_related_field().name}) |
114 | | url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()]) |
| 109 | params = self.url_parameters() |
| 110 | if params: |
| 111 | url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in params.items()]) |
| 112 | else: |
| 113 | url = '' |
115 | 114 | if not attrs.has_key('class'): |
116 | 115 | attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook. |
117 | 116 | output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)] |
… |
… |
class ForeignKeyRawIdWidget(forms.TextInput):
|
123 | 122 | if value: |
124 | 123 | output.append(self.label_for_value(value)) |
125 | 124 | return mark_safe(u''.join(output)) |
126 | | |
| 125 | |
| 126 | def url_parameters(self): |
| 127 | from django.contrib.admin.views.main import TO_FIELD_VAR |
| 128 | params = {} |
| 129 | if self.rel.limit_choices_to: |
| 130 | params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()])) |
| 131 | params.update({TO_FIELD_VAR: self.rel.get_related_field().name}) |
| 132 | return params |
| 133 | |
127 | 134 | def label_for_value(self, value): |
128 | 135 | key = self.rel.get_related_field().name |
129 | 136 | obj = self.rel.to.objects.get(**{key: value}) |
… |
… |
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
144 | 151 | else: |
145 | 152 | value = '' |
146 | 153 | return super(ManyToManyRawIdWidget, self).render(name, value, attrs) |
147 | | |
| 154 | |
| 155 | def url_parameters(self): |
| 156 | params = {} |
| 157 | if self.rel.limit_choices_to: |
| 158 | params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()])) |
| 159 | return params |
| 160 | |
148 | 161 | def label_for_value(self, value): |
149 | 162 | return '' |
150 | 163 | |
diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
index 2bc9075..19e2696 100644
a
|
b
|
Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">alb
|
69 | 69 | >>> rel = Album._meta.get_field('band').rel |
70 | 70 | >>> w = ForeignKeyRawIdWidget(rel) |
71 | 71 | >>> print conditional_escape(w.render('test', band.pk, attrs={})) |
72 | | <input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Linkin Park</strong> |
| 72 | <input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Linkin Park</strong> |
73 | 73 | |
74 | 74 | >>> m1 = Member.objects.create(pk=1, name='Chester') |
75 | 75 | >>> m2 = Member.objects.create(pk=2, name='Mike') |
… |
… |
True
|
100 | 100 | >>> rel = Inventory._meta.get_field('parent').rel |
101 | 101 | >>> w = ForeignKeyRawIdWidget(rel) |
102 | 102 | >>> print w.render('test', core.parent_id, attrs={}) |
103 | | <input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/admin_media/img/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Apple</strong> |
| 103 | <input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Apple</strong> |
104 | 104 | """ % { |
105 | 105 | 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, |
106 | 106 | 'STORAGE_URL': default_storage.url(''), |