0

I have a form where I want to be able to let people choose a country, region or city using 1 form field (so they only have to choose 1). I am using django-autocomplete-light, so my plan is to combine all 3 model fields into 1 form field - the user searches and selects the one they want and it gets saved to the relevant model field.

Done a lot of searching, but can't find anything on this - any thoughts?

Here is my form currently, with each field separate:

class PostForm(forms.ModelForm):
    country = forms.ModelChoiceField(Country.objects.all(),
        widget=autocomplete_light.ChoiceWidget('CountryAutocomplete',
        attrs={
        'minimum_characters': 1,
    }))
    region = forms.ModelChoiceField(Region.objects.all(),
        widget=autocomplete_light.ChoiceWidget('RegionAutocomplete',
        attrs={
        'minimum_characters': 1,
    }))
    city = forms.ModelChoiceField(City.objects.all(),
        widget=autocomplete_light.ChoiceWidget('CityAutocomplete',
        attrs={
        'minimum_characters': 1,
    }))

    class Meta:
        model = Post
        exclude = ('user', 'active',)

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                '',
                'post',
                'link',
                'country',
                'region',
                'city',
            ),
            ButtonHolder(
                Submit('submit', 'Submit', css_class='btn btn-success')
            )
        )
        super(PostForm, self).__init__(*args, **kwargs)

as you may see, I'm using django-autocomplete-light and django-crispy-forms

Not sure where to start on this one, so guidance and examples would be fantastic!!

Pete Drennan
  • 522
  • 1
  • 6
  • 15
  • 1
    If you have 3 Fk fields in your model Post to Country, Region, City models, so your form should save object properly ... or I don't understand what do you want `combine all 3 model fields into 1 form field` – madzohan Oct 22 '14 at 08:42
  • If you want 'chain' filter betwen these fields http://django-autocomplete-light.readthedocs.org/en/latest/dependant.html – madzohan Oct 22 '14 at 08:48
  • 1
    What I am after is 1 input box, where you can start typing and options from City, Region and Country all appear - you select one and it doesn't matter which one. – Pete Drennan Oct 22 '14 at 10:28
  • I think it is what you want http://django-autocomplete-light.readthedocs.org/en/latest/navigation.html – madzohan Oct 22 '14 at 10:38
  • I saw this and wanted it for a search box, but would this also work as an input for a form to save to the database? (I'll have to explore it more)... – Pete Drennan Oct 22 '14 at 10:44
  • Just change urls in template to you CreateView (or something else) and provide kwargs, something like this `{% for country in countrys %} {{ country }} {% endfor %}` and two other for-loops with link to the same url, but with different kwargs region = region.pk and later city=city.pk . In your urls.py provide `url(r'^some/url/(?P\d+)?(?P\d+)?(?P\d+)?/$', views.some_view, name='create_obj_view'),` – madzohan Oct 22 '14 at 11:02
  • Brilliant, I'll give it a crack and report back! – Pete Drennan Oct 22 '14 at 11:48
  • remove last slash before $, because if there pass none of kwargs there will be two slashes in the end // of url – madzohan Oct 22 '14 at 11:56

0 Answers0