[Answer]-Autocomplete-Light channel name conflict

1👍

What’s happening is that the channel class is generated in most cases and it’s name is generated too. However, you can avoid channel class generation and channel name generation (hopefully, or this would really suck).

From the registry documentation:

Three cases are possible:

  • specify model class and ModelNameChannel will be generated extending ChannelBase, with attribute model=model
  • specify a model and a channel class that does not have a model attribute, and a ModelNameChannel will be generated, with attribute
    model=model
  • specify a channel class with a model attribute, and the channel is directly registered

The solution to avoid channel class generation is to be in the third case: register a model and channel class with a model attribute.

autocomplete_light_registry.py

from sales.models import Item as SalesItem
from expenses.models import Item as ExpenseItem

class ExpenseChannel(autocomplete_light.ChannelBase):
    placeholder='Select an item (e)'
    model = ExpenseItem

    def query_filter(self, results):
        q = self.request.GET.get('q', None)

        if q:
            if results.model == ExpenseItem:
                results = results.filter(
                    Q(name__icontains=q)
    return results

class SalesChannel(autocomplete_light.ChannelBase):
    model = SalesItem
    placeholder = 'Select an item (s)'

    def query_filter(self, results):
        q = self.request.GET.get('q', None)

        if q:
            if results.model == SalesItem:
                results = results.filter(
                    Q(name__icontains=q)
    return results

autocomplete_light.register(ExpenseChannel)
autocomplete_light.register(SalesChannel)

That would work up to 0.7rc2.

Starting 0.7rc3 (to be released when the pending issue is closed), register() has a new keyword argument, channel_name, which you may use.

But you should be careful with your code, it seems like the query_filter() implementation from your classes is the same as the default implementation …

👤jpic

Leave a comment