88from tagging import settings
99from tagging .models import Tag
1010from tagging .utils import edit_string_for_tags
11+ from tagging .forms import TagField as TagFormField
12+
1113
1214class TagField (CharField ):
1315 """
@@ -18,8 +20,6 @@ class TagField(CharField):
1820 def __init__ (self , * args , ** kwargs ):
1921 kwargs ['max_length' ] = kwargs .get ('max_length' , 255 )
2022 kwargs ['blank' ] = kwargs .get ('blank' , True )
21- kwargs ['default' ] = kwargs .get ('default' , '' )
22- self ._initialized = False
2323 super (TagField , self ).__init__ (* args , ** kwargs )
2424
2525 def contribute_to_class (self , cls , name ):
@@ -54,31 +54,34 @@ class Link(models.Model):
5454 if instance is None :
5555 return edit_string_for_tags (Tag .objects .usage_for_model (owner ))
5656
57+ tags = self ._get_instance_tag_cache (instance )
58+ if tags is None :
59+ if instance .pk is None :
60+ self ._set_instance_tag_cache (instance , '' )
61+ else :
62+ self ._set_instance_tag_cache (
63+ instance , edit_string_for_tags (
64+ Tag .objects .get_for_object (instance )))
5765 return self ._get_instance_tag_cache (instance )
5866
5967 def __set__ (self , instance , value ):
6068 """
6169 Set an object's tags.
6270 """
6371 if instance is None :
64- raise AttributeError (_ ('%s can only be set on instances.' ) % self .name )
72+ raise AttributeError (
73+ _ ('%s can only be set on instances.' ) % self .name )
6574 if settings .FORCE_LOWERCASE_TAGS and value is not None :
6675 value = value .lower ()
6776 self ._set_instance_tag_cache (instance , value )
6877
69- def _save (self , ** kwargs ): # signal, sender, instance):
78+ def _save (self , ** kwargs ): # signal, sender, instance):
7079 """
7180 Save tags back to the database
7281 """
7382 tags = self ._get_instance_tag_cache (kwargs ['instance' ])
74- Tag .objects .update_tags (kwargs ['instance' ], tags )
75-
76- def _update (self , ** kwargs ): #signal, sender, instance):
77- """
78- Update tag cache from TaggedItem objects.
79- """
80- instance = kwargs ['instance' ]
81- self ._update_instance_tag_cache (instance )
83+ if tags is not None :
84+ Tag .objects .update_tags (kwargs ['instance' ], tags )
8285
8386 def __delete__ (self , instance ):
8487 """
@@ -90,31 +93,24 @@ def _get_instance_tag_cache(self, instance):
9093 """
9194 Helper: get an instance's tag cache.
9295 """
93- if not self ._initialized :
94- self ._initialized = True
95- self ._update (instance = instance )
9696 return getattr (instance , '_%s_cache' % self .attname , None )
9797
9898 def _set_instance_tag_cache (self , instance , tags ):
9999 """
100100 Helper: set an instance's tag cache.
101101 """
102+ # The next instruction does nothing particular,
103+ # but needed to by-pass the deferred fields system
104+ # when saving an instance, which check the keys present
105+ # in instance.__dict__.
106+ # The issue is introducted in Django 1.10
107+ instance .__dict__ [self .attname ] = tags
102108 setattr (instance , '_%s_cache' % self .attname , tags )
103109
104- def _update_instance_tag_cache (self , instance ):
105- """
106- Helper: update an instance's tag cache from actual Tags.
107- """
108- # for an unsaved object, leave the default value alone
109- if instance .pk is not None :
110- tags = edit_string_for_tags (Tag .objects .get_for_object (instance ))
111- self ._set_instance_tag_cache (instance , tags )
112-
113110 def get_internal_type (self ):
114111 return 'CharField'
115112
116113 def formfield (self , ** kwargs ):
117- from tagging import forms
118- defaults = {'form_class' : forms .TagField }
114+ defaults = {'form_class' : TagFormField }
119115 defaults .update (kwargs )
120116 return super (TagField , self ).formfield (** defaults )
0 commit comments