mlavin / django-selectable

Tools and widgets for using/creating auto-complete selection widgets using Django and jQuery UI.
http://django-selectable.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
129 stars 64 forks source link

“X” and “pencil” icon 404 #145

Open mhulse opened 9 years ago

mhulse commented 9 years ago

Hello,

Thanks for this awesome plugin (I'm using 0.9.0, Django 1.7.4 and Python 3.4.x).

Not sure if this is a feature or if I have things not fully setup/functional, but I noticed if I type-in a non-existent value (and the text turns red) and click the pencil icon or the red X buttons, I get a 404 page.

screen shot 2015-04-11 at 11 12 40 pm

Logically, I get why this happens. For folks that are not geeks, this could be confusing.

It would be pretty cool if the X and pencil would become inactive/disabled when the field turns red.

Thoughts?

Thanks again for sharing this code! It's the best auto complete I've found for the Django admin. :+1: :octocat:

mlavin commented 9 years ago

Thanks for the kind words! Just to be clear, are you are changing just the widget of a FK to use one of the autocomplete widgets to produce this behavior?

mhulse commented 9 years ago

Hi @mlavin, thanks so much for your reply.

Thanks for the kind words!

And thanks again for this awesome bit of code! It's a life saver. I wish this functionality was built-in to Django.

With that said, I hope this isn't just an issue due to my particular project setup? I have not tested in a fresh project/app, so I apologize in advance if this issue is specific to my setup.

Just to be clear, are you are changing just the widget of a FK to use one of the autocomplete widgets to produce this behavior?

I am using it on an FK and an inline FK. I'm on Django 1.7.x and Python 3.4.x. The pencil icon is from django-admin-enhancer, but her code (I think) is what made it into the 1.8 release of Django (which now has native pencil icons, which I think are turned off by default).

My code, if it's of any help:

lookups.py

from selectable.base import ModelLookup
from selectable.registry import registry

from .models import Product, Vehicle

# https://github.com/mlavin/django-selectable/blob/master/example/core/lookups.py

class ProductLookup(ModelLookup):

    model = Product

    search_fields = (
        'engine_model__icontains',
        'vehicle__model__icontains',
        'vehicle__engine_model__icontains',
        'vehicle__manufacturer__title__icontains',
    )

registry.register(ProductLookup)

class VehicleLookup(ModelLookup):

    model = Vehicle

    search_fields = (
        'model__icontains',
        'engine_model__icontains',
        'manufacturer__title__icontains',
    )

registry.register(VehicleLookup)

forms.py

from django import forms
from django.forms.models import modelformset_factory

import selectable.forms as selectable

from .lookups import ProductLookup, VehicleLookup
from .models import Quote, Lead

SELECTABLE_ATTRS = {

    'data-selectable-options': {
        'highlightMatch': False,
    },

}

class QuoteAdminForm(forms.ModelForm):

    class Meta(object):

        model = Quote

        widgets = {
            'product': selectable.AutoCompleteSelectWidget(lookup_class=ProductLookup, attrs=SELECTABLE_ATTRS,),
        }

        fields = ('product',)

FarmFormset = modelformset_factory(Quote, QuoteAdminForm)

class LeadAdminForm(forms.ModelForm):

    class Meta(object):

        model = Lead

        widgets = {
            'vehicle': selectable.AutoCompleteSelectWidget(lookup_class=VehicleLookup, attrs=SELECTABLE_ATTRS,),
        }

        fields = ('vehicle',)

FarmFormset = modelformset_factory(Lead, LeadAdminForm)

From there, I apply the forms to my admin classes to make everything functional.

Your code works great! I just noticed if I type in a non-existent item, and the field turns red, then click the X or pencil icon I get a 404 (via the local django test server).

Again, I apologize if this is an issue that's due to my setup. I probably should have tested your code in a fresh project/app before making a comment.

Are you able to reproduce this behaviour?

Thanks for your time!

mlavin commented 9 years ago

Thanks for the concrete example. I'll try it out and see if I can reproduce and get a fix in place.

mhulse commented 9 years ago

Thanks for the concrete example.

No problem at all. Sorry I forgot to provide that in my first post. :blush:

I'll try it out and see if I can reproduce and get a fix in place.

Wow, that's great! Thanks so much for your time on this. Please let me know if there's anything I can do to help code-wise and/or test with. Thanks @mlavin! :octocat:

Of course, there's no pressure to find a fix. I can easily tell people to avoid typing-in bad values. I suppose another fix would be to implement the "create" functionality (my code does not allow for new objects to be created, but maybe I could turn that on?).

Thanks again! This code has been extremely useful. :+1: :octocat:

mlavin commented 9 years ago

I don't see this behavior in 1.8. There appears to be some difference between django-admin-enhancer and what landed. In 1.8 it is specifically looking for a select box which changes. The edit and delete links are always non-functional (and pop up dismiss is once again broken shakes fist at Django). I'll try to get this working for 1.8 and hopefully that will fix it for django-admin-enhancer as well.

mhulse commented 9 years ago

Interesting! Thanks so much for checking. Please let me know if there is anything I can do to help.

I really appreciate your help and time. I owe you many tasty oregon micro-:beers: :octocat:! :)