AutoCompleteWidget that references two different lookup classes?

40 views
Skip to first unread message

brock1958

unread,
Jun 28, 2014, 6:34:48 PM6/28/14
to django-s...@googlegroups.com
I am trying to get the core example from the django-selectable examples to work with my database that contains a User class and a Groups class.It does seem to work fine in searching the Users model but I want to search the Groups as well as the Users. I am not sure how to reference two lookups to make this happen? It appears that the view is not even being call with the GET request when text is being keyed in. So I guess that referencing the two lookups in views.py is not the way to do it. But maybe within the forms.py?  Is there a way to do something like this:  widget=selectable.AutoCompleteWidget(FruitLookup, GroupsLookup) ???

Here are more details (notice the FruitLookup is straight from the example code and is really a Users lookup):

models.py:

from django.db import models

from django.utils import timezone

class Users(models.Model):

    displayname = models.CharField(max_length=255, db_column='displayName', unique=True)

    def __unicode__(self):

        return self.displayname

    class Meta:

        verbose_name_plural = "Users"

class Groups(models.Model):

    groupname = models.CharField(max_length=255, db_column='groupName', unique=True)

    def __unicode__(self):

        return self.groupshortname


    class Meta:

        verbose_name_plural = "Groups"


lookups.py:

from __future__ import unicode_literals

from selectable.base import ModelLookup

from selectable.registry import registry

from manitouincline.models import Users,  Groups

class FruitLookup(ModelLookup):

    model = Users

    search_fields = ('displayname__icontains', )

registry.register(FruitLookup)

class GroupsLookup(ModelLookup):

    model = Groups

    search_fields = ('groupname__icontains', )

registry.register(GroupsLookup)

forms.py:

from django import forms

import selectable

import selectable.forms as selectable

from manitouincline.models import Users, Climbs, Waypoints, Stats, Groups, Memberships

from manitouincline.lookups import GroupLookup, FruitLookup

class FruitForm(forms.Form):

    autocomplete = forms.CharField(

        label='Type the name of a fruit (AutoCompleteWidget)',

        widget=selectable.AutoCompleteWidget(FruitLookup),  <=== Is there a way to do something like this:  widget=selectable.AutoCompleteWidget(FruitLookup, GroupsLookup) ???

        required=False,

    )

views.py:

def example(request):

    if request.method == 'POST':

        form = FruitForm(request.POST)

    else:

        if request.GET:

            form = FruitForm(initial=request.GET)

            form2 = GroupsForm(initial=request.GET)

        else:

            form = FruitForm()

    raw_post = ''

    cleaned_data = ''

    if request.POST:

        raw_post = pprint.pformat(dict(request.POST))

        if form.is_valid():

            cleaned_data = pprint.pformat(getattr(form, 'cleaned_data', ''))

    context = {

        'cleaned_data': cleaned_data,

        'form': form,

        'raw_post': raw_post

    }

    return render_to_response('manitouincline/example.html', context, context_instance=RequestContext(request))

 
Any help would be appreciated!

Mark Lavin

unread,
Jun 28, 2014, 7:59:26 PM6/28/14
to django-s...@googlegroups.com
No as that is written it is not possible. However it is possible to create a lookup which queries two models by using the base lookup API and defining an appropriate `get_query` function http://django-selectable.readthedocs.org/en/v0.8.X/lookups.html#LookupBase.get_query You won't be able to use the ModelLookup to help you.

As for the view not being called for the auto-complete, the view which is called for the auto-completion is built into django-selectable. You won't see the selected value in your application until the form is finally submitted.

brock1958

unread,
Jun 29, 2014, 6:52:38 PM6/29/14
to django-s...@googlegroups.com
Mark,

Your reply was extremely helpful!

I was able to get the look up working using the get_query function as suggested:

class InclineLookup(ModelLookup):

  def get_query(self, request, term):

        groups = Groups.objects.filter(groupname__icontains=term)

        users = Users.objects.filter(displayname__icontains=term)

        search_results = []

        for group in groups:

                search_results.append(group)

        for user in users:

                search_results.append(user)

        return search_results

Thanks so much!
Reply all
Reply to author
Forward
0 new messages