(self, tags, *, through_defaults=None, **kwargs)
Set the object's tags to the given n tags. If the clear kwarg is True
        then all existing tags are removed (using `.clear()`) and the new tags
        added. Otherwise, only those tags that are not present in the args are
        removed and any new tags added.

        Any kwarg apart from 'clear' will be passed when adding tags.
Set the object's tags to the given n tags. If the clear kwarg is True
        then all existing tags are removed (using `.clear()`) and the new tags
        added. Otherwise, only those tags that are not present in the args are
        removed and any new tags added.
Set the object s tags to the given n tags . If the clear kwarg is True then all existing tags are removed ( using . clear () ) and the new tags added . Otherwise only those tags that are not present in the args are removed and any new tags added .
def set(self, tags, *, through_defaults=None, **kwargs):
        """
        Set the object's tags to the given n tags. If the clear kwarg is True
        then all existing tags are removed (using `.clear()`) and the new tags
        added. Otherwise, only those tags that are not present in the args are
        removed and any new tags added.

        Any kwarg apart from 'clear' will be passed when adding tags.

        """
        db = router.db_for_write(self.through, instance=self.instance)

        clear = kwargs.pop("clear", False)
        tag_kwargs = kwargs.pop("tag_kwargs", {})

        if clear:
            self.clear()
            self.add(*tags, **kwargs)
        else:
            # make sure we're working with a collection of a uniform type
            objs = self._to_tag_model_instances(tags, tag_kwargs)

            # get the existing tag strings
            old_tag_strs = set(
                self.through._default_manager.using(db)
                .filter(**self._lookup_kwargs())
                .values_list("tag__name", flat=True)
            )

            new_objs = []
            for obj in objs:
                if obj.name in old_tag_strs:
                    old_tag_strs.remove(obj.name)
                else:
                    new_objs.append(obj)

            self.remove(*old_tag_strs)
            self.add(*new_objs, through_defaults=through_defaults, **kwargs)
def set ( self , tags , * , through_defaults = None , * * kwargs ) : db = router . db_for_write ( self . through , instance = self . instance ) clear = kwargs . pop ( "clear" , False ) tag_kwargs = kwargs . pop ( "tag_kwargs" , { } ) if clear : self . clear ( ) self . add ( * tags , * * kwargs ) else : # make sure we're working with a collection of a uniform type objs = self . _to_tag_model_instances ( tags , tag_kwargs ) # get the existing tag strings old_tag_strs = set ( self . through . _default_manager . using ( db ) . filter ( * * self . _lookup_kwargs ( ) ) . values_list ( "tag__name" , flat = True ) ) new_objs = [ ] for obj in objs : if obj . name in old_tag_strs : old_tag_strs . remove ( obj . name ) else : new_objs . append ( obj ) self . remove ( * old_tag_strs ) self . add ( * new_objs , through_defaults = through_defaults , * * kwargs )