Search code examples
hyperlinkdjango-admineditdjango-autocomplete-light

How to add a link to django-autocomplete-light selected items?


New bie here. I successfully installed django-autocomplete-light to my django-admin v1.4.

Is there a way to add a hyperlink after selecting an autocomplete-light widget which is a foreignkey field in models? I can only see an x icon to cancel the selected item.

The purpose of creating a link is to open a pop window to edit the records selected.

Thanks in advance for any advice


Solution

  • You have to add this '?_popup=1" target="_blank" onclick="return showAddAnotherPopup(this);' to your url.

    Here is a complete solution.

    `class EditModelBase(autocomplete_light.AutocompleteModelBase):

    choice_html_format = u'''
        <span class="div" data-value="%s">%s</span>
        <a href="%s" title="%s"><img src="%s%s" /></a>
    '''
    
    def choice_html(self, choice):
        """
        Return a choice formated according to self.choice_html_format.
        """
        choice_format = u'''<span class="div" data-value="%s">%s</span>'''
        if not choice.get_absolute_update_url():
            return choice_format
        return self.choice_html_format % (
            self.choice_value(choice), self.choice_label(choice),
            choice.get_absolute_update_url(), _(u'Update'),
            settings.STATIC_URL, 'admin/img/icon_changelink.gif')`
    

    And:

    def get_absolute_update_url(self):
        url = reverse('admin:ccad_carrier_change', args=(self.id,))
        url = '%s?_popup=1" target="_blank" onclick="return showAddAnotherPopup(this);' % url
        return url
    

    I hope it works.