Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8787 -- Fixed failing admin_widgets tests. This fixes ManyToMa…

…nyRawIdWidget which was broken with [8823]. Thanks Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8846 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0cd5caa6a853bc12d6b09aba08e9d70f1d59c667 1 parent 67e141a
Brian Rosner authored September 02, 2008
30  django/contrib/admin/widgets.py
@@ -105,13 +105,12 @@ def __init__(self, rel, attrs=None):
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)]
@@ -123,7 +122,19 @@ def render(self, name, value, attrs=None):
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 base_url_parameters(self):
  127
+        params = {}
  128
+        if self.rel.limit_choices_to:
  129
+            params.update(dict([(k, ','.join(v)) for k, v in self.rel.limit_choices_to.items()]))
  130
+        return params    
  131
+    
  132
+    def url_parameters(self):
  133
+        from django.contrib.admin.views.main import TO_FIELD_VAR
  134
+        params = self.base_url_parameters()
  135
+        params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
  136
+        return params
  137
+            
127 138
     def label_for_value(self, value):
128 139
         key = self.rel.get_related_field().name
129 140
         obj = self.rel.to.objects.get(**{key: value})
@@ -144,7 +155,10 @@ def render(self, name, value, attrs=None):
144 155
         else:
145 156
             value = ''
146 157
         return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
147  
-
  158
+    
  159
+    def url_parameters(self):
  160
+        return self.base_url_parameters()
  161
+    
148 162
     def label_for_value(self, value):
149 163
         return ''
150 164
 
4  tests/regressiontests/admin_widgets/models.py
@@ -69,7 +69,7 @@ def __unicode__(self):
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>&nbsp;<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>&nbsp;<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')
@@ -100,7 +100,7 @@ def __unicode__(self):
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>&nbsp;<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>&nbsp;<strong>Apple</strong>
104 104
 """ % {
105 105
     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
106 106
     'STORAGE_URL': default_storage.url(''),

0 notes on commit 0cd5caa

Please sign in to comment.
Something went wrong with that request. Please try again.