这对我有用django-select2==7.1.1
#models.py
from django.db import models
class Tag(models.Model):
name = models.CharField(('Name'), max_length=255, unique=True)
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=255)
tags = models.ManyToManyField('Tag', blank=True)
#forms.py
from django_select2.forms import ModelSelect2TagWidget
class ArticleTagSelect2TagWidget(ModelSelect2TagWidget):
"""
Widget class for auto populate, edit & add tags.
"""
queryset = Tag.objects.all()
search_fields = ('name__icontains',)
@property
def empty_label(self):
return 'Type in tags'
def value_from_datadict(self, data, files, name):
'''Create objects for given non-pimary-key values. Return list of all names as name is the to_field_name.'''
values = set(super().value_from_datadict(data, files, name))
# This may only work for Tag, if Tag has title field.
# You need to implement this method yourself, to ensure proper object creation.
names = self.queryset.filter(**{'name__in': list(values)}).values_list('name', flat=True)
cleaned_values = list(names)
for val in values - set(list(names)):
cleaned_values.append(self.queryset.create(name=val).name)
return cleaned_values
class AddArticleForm(forms.ModelForm):
tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),
widget=ArticleTagSelect2TagWidget(data_view='dashboard:auto-json'),
required=False, to_field_name='name',)
class Meta:
model = Article
#views.py
AutoResponseView 被覆盖以使 ajax 返回名称而不是 id。
from django_select2.views import AutoResponseView
class TagAutoResponseView(AutoResponseView):
def get(self, request, *args, **kwargs):
"""
This method is overriden for changing id to name instead of pk.
"""
self.widget = self.get_widget_or_404()
self.term = kwargs.get('term', request.GET.get('term', ''))
self.object_list = self.get_queryset()
context = self.get_context_data()
return JsonResponse({
'results': [
{
'text': self.widget.label_from_instance(obj),
'id': obj.name,
}
for obj in context['object_list']
],
'more': context['page_obj'].has_next()
})