We use django-select2 for a project at work for managing tags. So now we use like this:
tags = ModelSelect2MultipleField(queryset=Tag.objects, required=False)
So it only works for existing tags, But it would be closer to the model stackoverflow and if the tag does not exist it adds, I found this link Tagging with AJAX in select2 which allows to manage side js, I'd like to know if it is possible to use an option in django-select2 to add it to the generated js. I would also like to know if instead of using the id it is possible to use a different field and side views in a get_form_kwargs I make a get_or_create.
Thanks
Applegrew make a new release that implements the management of tags with the tag created if it does not exist in the table. So use AutoModelSelect2TagField:
from django_select2 import AutoModelSelect2TagField
class TagChoices(AutoModelSelect2TagField):
queryset = Tag.objects
search_fields = ['name__icontains']
def get_model_field_values(self, value):
return {'name': value }
class SimpleForm(forms.ModelForm):
tags = TagChoices(required=False)
Here is a small example of using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With