Autocomplete in Django

57 views
Skip to first unread message

Mreno sert

unread,
Sep 17, 2017, 12:40:16 AM9/17/17
to Django users
I have a small app in which I have form for taking 5 inputs as City1, City2,City3,City4 and City5 as the fields. I have written a code for auto completing it but A user can not enter the new fields. Only existing values can be used. How can I make it to accept a new field if not present in the database

Andréas Kühne

unread,
Sep 17, 2017, 5:30:14 AM9/17/17
to django...@googlegroups.com
That really depends on how you want it to work?

I would probably add an item on the dropdown that shows the current items in the database, with "Add city". If the user presses that you can have an AJAX call that adds the item to the database.

Regards,

Andréas

2017-09-17 6:23 GMT+02:00 Mreno sert <zen...@gmail.com>:
I have a small app in which I have form for taking 5 inputs as City1, City2,City3,City4 and City5 as the fields. I have written a code for auto completing it but A user can not enter the new fields. Only existing values can be used. How can I make it to accept a new field if not present in the database

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e9d44e3-156b-4f5d-80da-5a0cfd0930d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jani Tiainen

unread,
Sep 17, 2017, 5:35:59 AM9/17/17
to django...@googlegroups.com
Hi.

Besides the way Andréas described, there exist autocomplete fields that can accept arbitrary input you can send to backend without having to create special add phase.

Adding such functionality of course requires a custom field that can add nonexisting values to database.


17.9.2017 7.40 "Mreno sert" <zen...@gmail.com> kirjoitti:
I have a small app in which I have form for taking 5 inputs as City1, City2,City3,City4 and City5 as the fields. I have written a code for auto completing it but A user can not enter the new fields. Only existing values can be used. How can I make it to accept a new field if not present in the database

--

Melvyn Sopacua

unread,
Sep 17, 2017, 9:23:33 AM9/17/17
to django...@googlegroups.com
In particular, you need to override the ModelChoiceField that a
ModelForm creates automatically and replace it with a field that is
not based on ChoiceField at all or skips it's validation method (like
https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal_select2/fields.py#L34).

On Sun, Sep 17, 2017 at 11:35 AM, Jani Tiainen <red...@gmail.com> wrote:
> Hi.
>
> Besides the way Andréas described, there exist autocomplete fields that can
> accept arbitrary input you can send to backend without having to create
> special add phase.
>
> Adding such functionality of course requires a custom field that can add
> nonexisting values to database.
>
>
> 17.9.2017 7.40 "Mreno sert" <zen...@gmail.com> kirjoitti:
>>
>> I have a small app in which I have form for taking 5 inputs as City1,
>> City2,City3,City4 and City5 as the fields. I have written a code for auto
>> completing it but A user can not enter the new fields. Only existing values
>> can be used. How can I make it to accept a new field if not present in the
>> database
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/0e9d44e3-156b-4f5d-80da-5a0cfd0930d5%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHn91odu2EUFBMSUX0CfX2f6in%2BHOUMDnAwkT8-nKPUaOGCnxw%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua

Mreno sert

unread,
Sep 17, 2017, 5:27:03 PM9/17/17
to Django users
Thanks for the help...but I do not know ajax. Is there anything simpler like I heard about "create-field" option. Tried it , but not getting it done correctly.


On Sunday, September 17, 2017 at 3:00:14 PM UTC+5:30, Andréas Kühne wrote:
That really depends on how you want it to work?

I would probably add an item on the dropdown that shows the current items in the database, with "Add city". If the user presses that you can have an AJAX call that adds the item to the database.

Regards,

Andréas

2017-09-17 6:23 GMT+02:00 Mreno sert <zen...@gmail.com>:
I have a small app in which I have form for taking 5 inputs as City1, City2,City3,City4 and City5 as the fields. I have written a code for auto completing it but A user can not enter the new fields. Only existing values can be used. How can I make it to accept a new field if not present in the database

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Melvyn Sopacua

unread,
Sep 18, 2017, 4:14:38 AM9/18/17
to django...@googlegroups.com
Then you need to learn Ajax.
You can't do dynamic forms like this and not use Ajax.

I linked django-autocomplete-light for a reason: if you follow the
docs and use it, you can build things you need without knowing Ajax.
However, if it doesn't work or is creating problems because of
incorrect input, it is vital you know the foundation of Ajax to
troubleshoot.
> https://groups.google.com/d/msgid/django-users/2088e144-e2f5-4351-a9d8-62954467da06%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages