4

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

Charlesliam
  • 1,293
  • 3
  • 20
  • 36
  • [Similar issue was already been raised here.](https://github.com/yourlabs/django-autocomplete-light/issues/172) – Charlesliam Nov 14 '13 at 12:56

1 Answers1

4

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.

  • It's giving this error `Exception Type: AttributeError Exception Value: 'Carrier' object has no attribute 'get_absolute_update_url'` – Charlesliam Nov 14 '13 at 13:59
  • is this part really do not have a close double quote? `onclick="return showAddAnotherPopup(this);'` – Charlesliam Nov 14 '13 at 14:02
  • When I follow this `You have to add this '?_popup=1" target="_blank" onclick="return showAddAnotherPopup(this);' to your url.` the code works. But when I tried your subclass the error I wrote above appears. All in all it works. Your answer is really helpful. The users will have an option to see the details in pop up without redirecting. Thanks. If readers have other way to work this out I'll gladly vote you up. – Charlesliam Nov 14 '13 at 14:17