0

I am able to get auto-complete working.

django-version : 1.8

But i also want to create choices on the fly, and these choices should be saved in Tag model.

According to my understanding, remote.js is to be used.
Hence I have added the below snippet to autocomplete_light_registry.py :
'data-bootstrap':'rest_modal'
'autocomplete' : 'remote'

the below remote.js snippet is also being fired

$('body').on('initialize', '.autocomplete-light-widget[data-  bootstrap=rest_model]', function() {
    $(this).yourlabsWidget(yourlabs.RemoteAutocompleteWidget);
});


But still, the getValue function of remote.js is not being called

models.py:

class Document(models.Model):
    tags = models.ManyToManyField(Tag)

class Tag(models.Model):
    name = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(unique=True)
    def __str__(self):
        return self.name

autocomplete_light_registry.py:

autocomplete_light.register(Tag,
    search_fields=['name'],

    attrs={
       'autocomplete' : 'remote',
       'placeholder': 'Please enter related tags?',
        'data-autocomplete-minimum-characters': 1,
    },
    widget_attrs={
        'data-bootstrap':'rest_modal',
        'data-widget-maximum-values': 4,
        'class': 'modern-style',
    },
)

forms.py:

class DocUploadForm(autocomplete_light.ModelForm):
    class Meta:
        model = Document
        # widgets = {'tags' : autocomplete_light.MultipleChoiceWidget('TagAutocomplete')}
        autocomplete_fields = ('tags',)
        exclude = ['organization','private_user']

What am i missing here ?

Do ask, if more clarity is required in the question or in the code

Thanks in advance.

dreamer
  • 901
  • 2
  • 15
  • 38

1 Answers1

0

Remote.js is not necessary here.

  1. Implement object creation in your Autocomplete.post()
  2. Use your own JS to trigger it

Example app: https://github.com/yourlabs/django-autocomplete-light/tree/master/autocomplete_light/example_apps/create_choice_on_the_fly

Live Example: http://dal-yourlabs.rhcloud.com/admin/create_choice_on_the_fly/

jpic
  • 32,891
  • 5
  • 112
  • 113