Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing spyglass icon in raw_id_fields widget when updating to django 1.8 #360

Closed
odedfos opened this issue Apr 19, 2015 · 8 comments
Closed

Comments

@odedfos
Copy link

odedfos commented Apr 19, 2015

I'm not sure what causes this, but it is easily reproducible with a freshly installed django (1.8) app without any 3rd parties.
In the admin.py we have something like this (Author is foreign key of Book):

class BookAdmin(admin.ModelAdmin):
raw_id_fields = ('author', )

class AuthorAdmin(admin.ModelAdmin):
list_display = ['name',]

admin.site.register(Book, BookAdmin)
admin.site.register(Author, AuthorAdmin)

Adding suit (0.2.12) to installed apps will cause the spyglass icon to disappear. Remove it, and all is well.

@odedfos
Copy link
Author

odedfos commented Apr 21, 2015

The problem seems to be that in Django 1.8 the raw-id-field widget mechanism was changed from an HTML element to a css rule + event hooks for the "related-lookup" class.

The following patch does the trick, though probably not the best solution to fit the package architecture.

Add the missing js code to change_form.html

{% block admin_change_form_document_ready %}
<script type="text/javascript">
    (function($) {
        $(document).ready(function() {
            $('.add-another').click(function(e) {
                e.preventDefault();
                showAddAnotherPopup(this);
            });
            $('.related-lookup').click(function(e) {
                e.preventDefault();
                showRelatedObjectLookupPopup(this);
            });
        {% if adminform and add %}
            $('form#{{ opts.model_name }}_form :input:visible:enabled:first').focus()
        {% endif %}
        });
    })(django.jQuery);
</script>
{% endblock %}

And the following css rules:

.add-another, .related-lookup {
margin-left: 5px;
display: inline-block;
}

.related-lookup {
width: 16px;
height: 16px;
background-image: url(../admin/img/selector-search.gif);
background-repeat: no-repeat;
}

@odedfos
Copy link
Author

odedfos commented Apr 26, 2015

This fix is not sufficient.

When clicking the magnifying glass icon, the browser is redirected to the relevant admin list view, which is wrong.
The Correct behavior is for the list view to appear in a popup, and selecting an item from it should set the value in the widget.

@marcwebbie
Copy link

I am experiencing the same issue with raw_id_fields, the lookup spyglass opens up a new page and not the expected popup as in the suit demo page

django==1.8
django-suit==0.2.13

We managed to make it work like this:

{% block extrahead %}
 <script type="text/javascript"><!--
   $( function() {
     $( '.related-lookup:not([onclick])' ).click( function() {
       return showRelatedObjectLookupPopup( this ) ;
     }) ;
   } ) ;
 //--></script>
 {% endblock %}

@mcnemesis
Copy link

@marcwebbie thanks! I'm not actually using Django-Suit (using Django Admin Bootstrapped), but that same solution as you indicate worked for me as well.

@calebjoseph
Copy link

Hi @marcwebbie , could you please let me know which file you placed the block extrahead code into?
Thanks!

p.s. @darklow I've tried 0.2.13, 0.2.14 and 0.2.15 but all do not work for the raw id field popup

@marcwebbie
Copy link

Hello @calebjoseph and @darklow,

I've added this snippet on static/suit/js/custom.js and loaded it on the templates/admin/base_site.html:

{% block extrajs %}
  <script type="text/javascript" src="{% static 'suit/js/custom.js' %}"></script>
{% endblock %}

@calebjoseph
Copy link

Thanks @marcwebbie , works like a charm.

@timbadu
Copy link

timbadu commented Jan 11, 2017

Also, don't forget to upgrade your admin jquery.
../admin/static/admin/js/jquery.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants