AutoCompleteWidget, AutoCompleteSelectField question

46 views
Skip to first unread message

Sylvain Martel

unread,
Feb 26, 2016, 11:34:23 AM2/26/16
to django-selectable
Novice here.
I've been fiddling with django-selectable to do an autocomplete when searching for a user, and allowing for the user to be created if not found.
Correct me if I'm wrong but it looks like:

- If I use AutoCompleteWidget, I can define get_item_value to decide what is returned, but the setting allow_new = True won't call create_item()
- If I use AutoCompleteSelectField, it will call create_item(), but it will actually ignore get_item_value?(at least that is what is happening.)

Is there a way to combine both behavior so that:
When a user is found, it will actually return the username instead of the full name.  And if no user is found, the call create_item() will be called.

Here is my lookup:

class MyUserLookup(ModelLookup):
    model = MyUsers
    search_fields = ('fullname__istartswith', )

    def get_item_value(self, item):
        # Display for currently selected item
        return item.username

    def get_item_label(self, item):
        # Display for choice listings
        return "%s (%s)" % (item.fullname, item.username)

    def create_item(self, value):
        # simply return a string to tell the view to redirect to create a new member
        return "Create_new_member"

registry.register(MyUserLookup)

Mark Lavin

unread,
Feb 26, 2016, 12:09:24 PM2/26/16
to django-s...@googlegroups.com
If you want to create new objects then using the AutoCompleteWidget does not make sense. This is for completing text, not selecting objects. The value from the cleaned_data will be what is returned by get_item_value.

AutoCompleteSelectField can work for this purpose. It does use get_item_value to display the value if a user selects an existing item on the front-end. I'm not sure what you mean when you say it's being ignored.

--
You received this message because you are subscribed to the Google Groups "django-selectable" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-selecta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sylvain Martel

unread,
Feb 26, 2016, 1:33:19 PM2/26/16
to django-selectable
Hi,

Ok, so AutoCompleteSelectField it should be.

What I mean by 'it's being ignored' is that when I use "
AutoCompleteSelectField", it will display the username in the box, as it should, but the actual value returned to the view is the full name.

As an example:
Username | Fullname
Willow      | Sylvain Martel

So I type "Syl" in the autocomplete box.  It shows me "Sylvain Martel"  I select it, it then writes "Willow" in the autocomplete box.  All is fine so far.  So I click submit.
But in the view, the variable member_username = data["name"] is receiving the fullname(Sylvain Martel) instead of the username(Willow).

With AutoCompleteWidget, the variable will receive the username as expected .

Mark Lavin

unread,
Feb 26, 2016, 1:43:27 PM2/26/16
to django-s...@googlegroups.com
The value in the clean_data when using the AutoCompleteSelectField should be a user object, not a string.

Sylvain Martel

unread,
Feb 26, 2016, 1:58:25 PM2/26/16
to django-selectable
Oh!

Everything works beautifully now by treating the return as an object instead of a string.   Usually, a print() will tell when it's an object, but I forgot I added a def __str__(self):return self.fullname to the model.  Hence why it was showing me the fullname while trying to debug the problem.

Huge thanks for your time! 

Mark Lavin

unread,
Feb 26, 2016, 1:59:19 PM2/26/16
to django-s...@googlegroups.com
Happy to help! Glad you got it working.
Reply all
Reply to author
Forward
0 new messages