Ticket #8787: django-admin-widget-test.diff

File django-admin-widget-test.diff, 4.6 KB (added by Alex Gaynor, 16 years ago)
  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index 50e55dc..6a14b66 100644
    a b class ForeignKeyRawIdWidget(forms.TextInput):  
    105105        super(ForeignKeyRawIdWidget, self).__init__(attrs)
    106106
    107107    def render(self, name, value, attrs=None):
    108         from django.contrib.admin.views.main import TO_FIELD_VAR
    109108        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 = ''
    115114        if not attrs.has_key('class'):
    116115          attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
    117116        output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
    class ForeignKeyRawIdWidget(forms.TextInput):  
    123122        if value:
    124123            output.append(self.label_for_value(value))
    125124        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           
    127134    def label_for_value(self, value):
    128135        key = self.rel.get_related_field().name
    129136        obj = self.rel.to.objects.get(**{key: value})
    class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):  
    144151        else:
    145152            value = ''
    146153        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   
    148161    def label_for_value(self, value):
    149162        return ''
    150163
  • tests/regressiontests/admin_widgets/models.py

    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  
    6969>>> rel = Album._meta.get_field('band').rel
    7070>>> w = ForeignKeyRawIdWidget(rel)
    7171>>> 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>
    7373
    7474>>> m1 = Member.objects.create(pk=1, name='Chester')
    7575>>> m2 = Member.objects.create(pk=2, name='Mike')
    True  
    100100>>> rel = Inventory._meta.get_field('parent').rel
    101101>>> w = ForeignKeyRawIdWidget(rel)
    102102>>> 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>
    104104""" % {
    105105    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
    106106    'STORAGE_URL': default_storage.url(''),
Back to Top