3

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

4

2 回答 2

2

Applegrew 发布了一个新版本,使用表中不存在时创建的标签来实现标签的管理。所以使用 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)

这是一个使用的小例子。

于 2013-09-16T14:31:24.790 回答
2

这对我有用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.pyAutoResponseView 被覆盖以使 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()
        })
于 2019-09-03T05:17:45.547 回答