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

'set' object does not support indexing #93

Closed
openrijal opened this issue Oct 24, 2014 · 6 comments
Closed

'set' object does not support indexing #93

openrijal opened this issue Oct 24, 2014 · 6 comments

Comments

@openrijal
Copy link

Hello.

I'm trying to use make_ajax_field() for the autocompletion. I have followed the installation instruction as well as the example app thoroughly. I keep getting the error 'set' object does not support indexing. What is this related to? Any insight would be very helpful.

These are my settings and codes:

settings.py

Settings for ajax_select

AJAX_LOOKUP_CHANNELS = {
'client_lookup': {'client.lookups','ClientLookup'},
'driver_lookup': {'driver.lookups','DriverLookup'},
}

admin.py

class LoadingAdmin(AjaxSelectAdmin):
form = LoadingForm

forms.py

from django import forms
from ajax_select import make_ajax_field
from data_entry.models import Loading

class LoadingForm(forms.ModelForm):

class Meta:
    model = Loading

client = make_ajax_field(Loading, 'client', 'client_lookup')

lookups.py

from django.db.models import Q
from django.utils.html import escape
from client.models import Client
from ajax_select import LookupChannel

class ClientLookup(LookupChannel):

model = Client

def get_query(self, q, request):
    return Client.objects.filter(Q(name__icontains=q)).order_by('name')

def get_result(self, obj):
    u""" result is the simple text that is the completion of what the person typed """
    return obj.name

def format_match(self, obj):
    """ (HTML) formatted item for display in the dropdown """
    return u"%s<div><i>%s</i></div>" % (escape(obj.name), escape(obj.address))
    # return self.format_item_display(obj)

def format_item_display(self, obj):
    """ (HTML) formatted item for displaying item in the selected deck area """
    return u"%s<div><i>%s</i></div>" % (escape(obj.name), escape(obj.address))

PS: I have enabled db_index=True in the 'name' field that I'm trying to search.

@crucialfelix
Copy link
Owner

Something is expecting a list and instead its getting a set.
https://docs.python.org/2/library/sets.html#set-objects

maybe this is the problem:

Client.objects.filter(Q(name__icontains=q))

Q should always be used with | to mean "OR"
If you have only one then it makes no sense, it should just be .filter(name__iconstains=q)

so it would be Django that is choking. look at the full stack trace to see where the error happens

@openrijal
Copy link
Author

Thanks.

Just removed the .objects.filter(Q(name__icontains=q)) with .objects.filter(name__icontains=q)

The error still remains. I have pasted my full stack trace here: http://pastebin.com/Mn4utdZv

From what I see, the error is shown in .get_form(), but can't seem to identify it.

Please have a look if you can help me with this.

Regards.

@crucialfelix
Copy link
Owner

oh I see it:

this is a python set:

{'client.lookups','ClientLookup'},

this is a python dict:

{'client.lookups': 'ClientLookup'},

, vs :

I've been caught by that one before.

greetings to nepal ! I'll make it there someday soon

@openrijal
Copy link
Author

Thanks, it worked.
This issue is resolved, while I ran into another one which just says 'model'

Exception Type: KeyError
Exception Value: 'model'
Exception Location: /home/openrijal/.virtualenvs/rungta/lib/python2.7/site-packages/ajax_select/init.py in get_lookup, line 177

It still concerns that very file. I think I should create a new issue with it. The full stack trace is here: http://pastebin.com/iad9GxGT

@openrijal
Copy link
Author

I made it work the simple way, instead of using Forms and Lookups.

This solved my problem for now. You might want to change the comma to colon in your instructions.

Cheers.

@crucialfelix
Copy link
Owner

the README says:

# define the lookup channels in use on the site
AJAX_LOOKUP_CHANNELS = {
    #  simple: search Person.objects.filter(name__icontains=q)
    'person'  : {'model': 'example.person', 'search_field': 'name'},
    # define a custom lookup channel
    'song'   : ('example.lookups', 'SongLookup')
}

there are two ways to specify. by tuple or by dict

I'll make the instructions clearer, since its too easy to not notice such a
small thing as a comma

btw. when you post a stacktrace click on the >

  1. Traceback Switch to copy-and-paste view

so I can see the whole stacktrace.

thanks,

On Fri, Oct 24, 2014 at 12:52 PM, Nitesh Rijal notifications@github.com
wrote:

I made it work the simple way, instead of using Forms and Lookups.

This solved my problem for now. You might want to change the comma to
colon in your instructions.

Cheers.


Reply to this email directly or view it on GitHub
#93 (comment)
.

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

2 participants