Python taggit.managers.TaggableManager() Examples

The following are 6 code examples of taggit.managers.TaggableManager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module taggit.managers , or try the search function .
Example #1
Source File:    From peering-manager with Apache License 2.0 5 votes vote down vote up
def to_dict(self):
        data = {}

        # Iterate over croncrete and many-to-many fields
        for field in self._meta.concrete_fields + self._meta.many_to_many:
            value = None

            # If the value of the field is another model, fetch an instance of it
            # Exception made of tags for which we just retrieve the list of them for later
            # conversion to simple strings
            if isinstance(field, ForeignKey):
                value = self.__getattribute__(
            elif isinstance(field, ManyToManyField):
                value = list(self.__getattribute__(
            elif isinstance(field, TaggableManager):
                value = list(self.__getattribute__(
                value = self.__getattribute__(

            # If the instance of a model as a to_dict() function, call it
            if isinstance(value, TemplateModel):
                data[] = value.to_dict()
            elif isinstance(value, list):
                data[] = []
                for element in value:
                    if isinstance(element, TemplateModel):
                data[] = value

        return data 
Example #2
Source File:    From django-modelcluster with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __get__(self, instance, model):
        # override TaggableManager's requirement for instance to have a primary key
        # before we can access its tags
        manager = _ClusterTaggableManager(
            through=self.through, model=model, instance=instance,

        return manager 
Example #3
Source File:    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_comparison_class(self):
        # Hide fields with hidden widget
        widget_override = self.widget_overrides().get(self.field_name, None)
        if widget_override and widget_override.is_hidden:

            field = self.db_field

            if field.choices:
                return compare.ChoiceFieldComparison

            if field.is_relation:
                if isinstance(field, TaggableManager):
                    return compare.TagsFieldComparison
                elif field.many_to_many:
                    return compare.M2MFieldComparison

                return compare.ForeignObjectComparison

            if isinstance(field, RichTextField):
                return compare.RichTextFieldComparison

            if isinstance(field, (CharField, TextField)):
                return compare.TextFieldComparison

        except FieldDoesNotExist:

        return compare.FieldComparison 
Example #4
Source File:    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def filter_queryset(self, request, queryset, view):
        This performs field level filtering on the result set
        Eg: ?title=James Joyce
        fields = set(view.get_available_fields(queryset.model, db_fields_only=True))

        for field_name, value in request.GET.items():
            if field_name in fields:
                    field = queryset.model._meta.get_field(field_name)
                except LookupError:
                    field = None

                # Convert value into python
                    if isinstance(field, (models.BooleanField, models.NullBooleanField)):
                        value = parse_boolean(value)
                    elif isinstance(field, (models.IntegerField, models.AutoField)):
                        value = int(value)
                except ValueError as e:
                    raise BadRequestError("field filter error. '%s' is not a valid value for %s (%s)" % (

                if isinstance(field, TaggableManager):
                    for tag in value.split(','):
                        queryset = queryset.filter(**{field_name + '__name': tag})

                    # Stick a message on the queryset to indicate that tag filtering has been performed
                    # This will let the do_search method know that it must raise an error as searching
                    # and tag filtering at the same time is not supported
                    queryset._filtered_by_tag = True
                    queryset = queryset.filter(**{field_name: value})

        return queryset 
Example #5
Source File:    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_value(self, obj):
        from taggit.managers import TaggableManager

            field = self.get_field(obj.__class__)
            value = field.value_from_object(obj)
            if hasattr(field, 'get_searchable_content'):
                value = field.get_searchable_content(value)
            elif isinstance(field, TaggableManager):
                # As of django-taggit 1.0, value_from_object returns a list of Tag objects,
                # which matches what we want
            elif isinstance(field, RelatedField):
                # The type of the ForeignKey may have a get_searchable_content method that we should
                # call. Firstly we need to find the field its referencing but it may be referencing
                # another RelatedField (eg an FK to page_ptr_id) so we need to run this in a while
                # loop to find the actual remote field.
                remote_field = field
                while isinstance(remote_field, RelatedField):
                    remote_field = remote_field.target_field

                if hasattr(remote_field, 'get_searchable_content'):
                    value = remote_field.get_searchable_content(value)
            return value
        except FieldDoesNotExist:
            value = getattr(obj, self.field_name, None)
            if hasattr(value, '__call__'):
                value = value()
            return value 
Example #6
Source File:    From peering-manager with Apache License 2.0 4 votes vote down vote up
def get_model_variables(model):
    variables = []

    for field in model._meta.concrete_fields + model._meta.many_to_many:
        variable = None

        if isinstance(field, ForeignKey):
            variable = "`{}`: {} object".format(
      , get_model_file_link(field.related_model)
        elif isinstance(field, ManyToManyField):
            variable = "`{}`: list of {} objects".format(
      , get_model_file_link(field.related_model)
        elif isinstance(field, TaggableManager):
            variable = "`{}`: list of tags".format(
            field_type = None
            if isinstance(field, BooleanField):
                field_type = "boolean"
            elif isinstance(field, CharField):
                field_type = "string"
            elif isinstance(field, TextField):
                field_type = "text"
            elif isinstance(field, DateTimeField):
                field_type = "date"
            elif isinstance(field, InetAddressField):
                field_type = "IP address"
            elif (
                isinstance(field, AutoField)
                or isinstance(field, BigIntegerField)
                or isinstance(field, PositiveIntegerField)
                or isinstance(field, PositiveSmallIntegerField)
                field_type = "integer".format(
                    "Unknown type for `{}`: {}".format(
              , field.get_internal_type()

            if field_type:
                variable = "`{}`: {} value".format(, field_type)

        if variable:

    return variables