Ticket #9522: 9522.patch
File 9522.patch, 3.8 KB (added by , 16 years ago) |
---|
-
django/db/models/fields/__init__.py
52 52 # Designates whether empty strings fundamentally are allowed at the 53 53 # database level. 54 54 empty_strings_allowed = True 55 56 # Designates whether this field's value must be converted to string 57 # by `value_to_string` method while serialization 58 serialize_to_string = False 55 59 56 60 # These track each time a Field instance is created. Used to retain order. 57 61 # The auto_creation_counter is used for fields that Django implicitly -
django/core/serializers/python.py
35 35 self._current = None 36 36 37 37 def handle_field(self, obj, field): 38 self._current[field.name] = smart_unicode(getattr(obj, field.name), strings_only=True) 38 if field.serialize_to_string: 39 value = self.get_string_value(obj, field) 40 else: 41 value = smart_unicode(getattr(obj, field.name), strings_only=True) 42 self._current[field.name] = value 39 43 40 44 def handle_fk_field(self, obj, field): 41 45 related = getattr(obj, field.name) -
tests/modeltests/serializers/models.py
67 67 class Score(models.Model): 68 68 score = models.FloatField() 69 69 70 71 class Team(object): 72 def __init__(self, title): 73 self.title = title 74 75 def __unicode__(self): 76 raise NotImplementedError("Not so simple") 77 78 def __str__(self): 79 raise NotImplementedError("Not so simple") 80 81 def to_string(self): 82 return "%s" % self.title 83 84 class TeamField(models.CharField): 85 __metaclass__ = models.SubfieldBase 86 87 serialize_to_string = True 88 89 def __init__(self): 90 super(TeamField, self).__init__(max_length=100) 91 92 def get_db_prep_save(self, value): 93 return unicode(value.title) 94 95 def to_python(self, value): 96 if isinstance(value, Team): 97 return value 98 return Team(value) 99 100 def value_to_string(self, obj): 101 return self._get_val_from_obj(obj).to_string() 102 103 class Player(models.Model): 104 rank = models.IntegerField() 105 team = TeamField() 106 107 def __unicode__(self): 108 return self.team.to_string() 109 70 110 __test__ = {'API_TESTS':""" 71 111 # Create some data: 72 112 >>> from datetime import datetime … … 217 257 >>> print list(serializers.deserialize('json', serializers.serialize('json', [sc])))[0].object.score 218 258 3.4 219 259 260 261 # Custom field with non trivial to string convertion value 262 >>> player = Player() 263 >>> player.rank = 1 264 >>> player.team = Team("Spartak Moskva") 265 >>> player.save() 266 267 >>> serialized = serializers.serialize("json", Player.objects.all()) 268 >>> print serialized 269 [{"pk": 1, "model": "serializers.player", "fields": {"rank": 1, "team": "Spartak Moskva"}}] 270 271 >>> obj = list(serializers.deserialize("yaml", serialized))[0] 272 >>> print obj 273 <DeserializedObject: Spartak Moskva> 220 274 """} 221 275 222 276 try: … … 253 307 <DeserializedObject: Just kidding; I love TV poker> 254 308 <DeserializedObject: Time to reform copyright> 255 309 310 # Custom field with non trivial to string convertion value with YAML serializer 311 312 >>> print serializers.serialize("yaml", Player.objects.all()) 313 - fields: {rank: 1, team: Spartak Moskva} 314 model: serializers.player 315 pk: 1 316 <BLANKLINE> 317 256 318 """ 257 319 except ImportError: 258 320 pass