yourlabs / django-cities-light

A simple app providing three models: Country, Region and City model. Also provided, a command to insert or update data from geonames database dumps. Status: stable.
http://django-cities-light.rtfd.org/
MIT License
335 stars 126 forks source link

How-to restrict cities_light autocomplete to display only Region USA cities? #48

Closed coderam closed 10 years ago

coderam commented 10 years ago

Here is my code:

class CityAutocomplete(autocomplete_light.AutocompleteModelBase):

https://github.com/yourlabs/django-autocomplete-light/issues/228

#search_fields = ['^name', ]
search_fields = ['search_names', 'alternate_names']

autocomplete_light.register(City, CityAutocomplete)

jpic commented 10 years ago

Well it depends, why do you want to display only a subset of regions in your database ?

Does the region subset depend on a request variable ? then http://django-autocomplete-light.readthedocs.org/en/v2/api.html?highlight=choices_for#autocomplete_light.autocomplete.model.AutocompleteModel.choices_for_request

Does the region subset depend on some other field value ? then http://django-autocomplete-light.readthedocs.org/en/v2/dependant.html

Do you have regions that you don't want in the database ? then http://django-cities-light.readthedocs.org/en/latest/database.html#module-cities_light.signals

coderam commented 10 years ago

Here is what I added just now -- for some reason, all countries are still visible. I am trying to filter only "US" cities:

class CityAutocomplete(autocomplete_light.AutocompleteModelBase):

https://github.com/yourlabs/django-autocomplete-light/issues/228

#search_fields = ['^name', ]
search_fields = ['search_names', 'alternate_names']
def choices_for_request(self):

    choices = self.choices.all()
    choices = choices.filter(country__code2='US')

    return self.order_choices(choices)[0:self.limit_choices]

autocomplete_light.register(City, CityAutocomplete)

On Mon, Mar 3, 2014 at 2:59 PM, James Pic notifications@github.com wrote:

Well it depends, why do you want to display only a subset of regions in your database ?

Does the region subset depend on a request variable ? then

http://django-autocomplete-light.readthedocs.org/en/v2/api.html?highlight=choices_for#autocomplete_light.autocomplete.model.AutocompleteModel.choices_for_request

Does the region subset depend on some other field value ? then http://django-autocomplete-light.readthedocs.org/en/v2/dependant.html

Do you have regions that you don't want in the database ? then

http://django-cities-light.readthedocs.org/en/latest/database.html#module-cities_light.signals

Reply to this email directly or view it on GitHubhttps://github.com/yourlabs/django-cities-light/issues/48#issuecomment-36552254 .

Alex

jpic commented 10 years ago

Then your filter doesn't work. Open the autocomplete url page directly for easier debugging. Also try import ipdb; ipdb.set_trace() in your choices_for_request() function. Also don't override it like that, it's better to do like this: http://django-autocomplete-light.readthedocs.org/en/v2/autocomplete.html#overriding-the-queryset-of-a-model-autocomplete-to-secure-an-autocomplete

coderam commented 10 years ago

Still not working -- all countries are showing up -- sorry, I'm not the best programmer ;)

class CityAutocomplete(autocomplete_light.AutocompleteModelBase):

https://github.com/yourlabs/django-autocomplete-light/issues/228

#search_fields = ['^name', ]
model = City
search_fields = ['search_names', 'alternate_names']
def choices_for_request(self):
    self.choices = self.choices.filter(country__code2='US')

    #return self.order_choices(choices)[0:self.limit_choices]
    return super(CityAutocomplete, self).choices_for_request()

autocomplete_light.register(City, CityAutocomplete)

On Mon, Mar 3, 2014 at 3:20 PM, James Pic notifications@github.com wrote:

Then your filter doesn't work. Open the autocomplete url page directly for easier debugging. Also try import ipdb; ipdb.set_trace() in your choices_for_request() function. Also don't override it like that, it's better to do like this: http://django-autocomplete-light.readthedocs.org/en/v2/autocomplete.html#overriding-the-queryset-of-a-model-autocomplete-to-secure-an-autocomplete

Reply to this email directly or view it on GitHubhttps://github.com/yourlabs/django-cities-light/issues/48#issuecomment-36554561 .

Alex

jpic commented 10 years ago

Ok, try to put import ipdb; ipdb.set_trace() at the beginning of that function.

My guess is that it's just not using that autocomplete class :D

coderam commented 10 years ago

You are right, it is not even calling that function. I tried placing the filter directly in the Form class:

class RentalSearchForm(Form):

city = forms.ModelChoiceField(City.objects.filter(*country__code2='US'*),

widget=autocomplete_light.ChoiceWidget('CityAutocomplete', attrs={'novalidate':'novalidate'}), required=False)

Still not working...

On Mon, Mar 3, 2014 at 3:31 PM, James Pic notifications@github.com wrote:

import ipdb; ipdb.set_trace()

Alex

jpic commented 10 years ago

Did you try opening /autocomplete/ or wherever you included autocomplete_light.urls to get the debug view ? (note: be logged as superadmin).

On Mon, Mar 3, 2014 at 9:41 PM, coderam notifications@github.com wrote:

You are right, it is not even calling that function. I tried placing the filter directly in the Form class:

class RentalSearchForm(Form):

city = forms.ModelChoiceField(City.objects.filter(_country_code2='US'), widget=autocomplete_light.ChoiceWidget('CityAutocomplete', attrs={'novalidate':'novalidate'}), required=False)

Still not working...

On Mon, Mar 3, 2014 at 3:31 PM, James Pic notifications@github.com wrote:

import ipdb; ipdb.set_trace()

Alex

— Reply to this email directly or view it on GitHubhttps://github.com/yourlabs/django-cities-light/issues/48#issuecomment-36556890 .

http://yourlabs.org http://blog.yourlabs.org Customer is king - Le client est roi - El cliente es rey.

coderam commented 10 years ago

On Mon, Mar 3, 2014 at 3:47 PM, James Pic notifications@github.com wrote:

autocomplete

Oh! I registered 2 different CityAutocomplete classes with the same name. One was over-riding the other... that's why I didn't see any changes happening...

Thanks!

Alex