0

I installed django-cities-light and django-autocomplete-light. All went well until I tried to prevent user from choosing Brussels as region if they entered France as country.

I followed the tutorial here : http://django-autocomplete-light.readthedocs.org/en/v2/dependant.html but I somehow missed an important detail.

The autocomplete app works fine but there is no restriction. As I don't know what the class media is supposed to be, I guess the problem could be that.Moreover, the docs says it is used to load some extra javascript code and it seems not to be loaded as the app works fine.

Here is my models.py

from django.db import models
from user.models import User
from cities_light.models import City, Country, Region

class Offer(models.Model):
    publisher = models.ForeignKey(User)
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True, auto_now=False)

    country = models.ForeignKey(Country)
    region = models.ForeignKey(Region)
    city = models.ForeignKey(City)

autocomplete_light_registry.py

import autocomplete_light
from offers.models import Offer
from cities_light.models import Country, Region, City

class CountryAutocomplete(autocomplete_light.AutocompleteModelBase):
    attrs={'placeholder': 'Choisissez votre Pays'}
    search_fields=('search_names', 'alternate_names',)

    def choices_for_request(self):
        q = self.request.GET.get('q', '')
        country_id = self.request.GET.get('country_id', None)

        choices = self.choices.all()
        if q:
            choices = choices.filter(name_ascii__icontains=q)
        if country_id:
            choices = choices.filter(country_id=country_id)

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

autocomplete_light.register(Country, CountryAutocomplete)

class RegionAutocomplete(autocomplete_light.AutocompleteModelBase):
    attrs={'placeholder': 'region name ..'}
    search_fields=('search_names', 'alternate_names',)

    def choices_for_request(self):
        q = self.request.GET.get('q', '')
        country_id = self.request.GET.get('country_id', None)

        choices = self.choices.all()
        if q:
            choices = choices.filter(name_ascii__icontains=q)
        if country_id:
            choices = choices.filter(country_id=country_id)

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

autocomplete_light.register(Region, RegionAutocomplete)


class CityAutocomplete(autocomplete_light.AutocompleteModelBase):
    attrs={'placeholder': 'Choisissez votre ville'}
    search_fields=('search_names', 'alternate_names',)

    def choices_for_request(self):
        q = self.request.GET.get('q', '')
        city_id = self.request.GET.get('city_id', None)

        choices = self.choices.all()
        if q:
            choices = choices.filter(name_ascii__icontains=q)
        if city_id:
            choices = choices.filter(country_id=country_id)

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

autocomplete_light.register(City, CityAutocomplete)

forms.py

from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

from offers.models import Offer, LevelChoices, CoursChoices

import autocomplete_light


class SendOfferForm(autocomplete_light.ModelForm):

    class Media:
        js = ('dependant_autocomplete.js',)
    class Meta:
        model = Offer
        autocomplete_fields = ("country", "region", "city")
        fields = ('country', 'region', 'city', 'content')

EDIT

I discovered that if I delete all the code after the attrs field in autocomplete_light_registry.py, nothing changes. Something is not recognize by django. I checked my settings.py but it seems ok

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')

1 Answer 1

0
  1. add 'autocomplete_light.example_apps.dependant_autocomplete', to your installed apps

  2. Access the newly added app via the admin interface

  3. Check if the autocomplete filters correctly
  4. if yes Done
  5. If no, connect your computer to the Internet and repeat steps 2 and 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.