hagsteel / swampdragon

swampdragon
Other
557 stars 74 forks source link

use value.url for both FieldFile and FileField #184

Open sachazyto opened 8 years ago

sachazyto commented 8 years ago

Using django 1.8.4, I'm getting the following error when trying to serialize a FileField: <FieldFile: sampleapp/img5_L3sBxJI.png> is not JSON serializable Traceback: [...] File "/home/vagrant/.venvs/infra_env/local/lib/python2.7/site-packages/swampdragon/models.py" in save

  1. self._publish(self.action, self.changed_fields) File "/home/vagrant/.venvs/infra_env/local/lib/python2.7/site-packages/swampdragon/models.py" in _publish
  2. publish_model(self, self._serializer, action, changed_fields) File "/home/vagrant/.venvs/infra_env/local/lib/python2.7/site-packages/swampdragon/pubsub_providers/model_publisher.py" in publish_model
  3. publisher.publish(c, publish_data) File "/home/vagrant/.venvs/infra_env/local/lib/python2.7/site-packages/swampdragon/pubsub_providers/redis_publisher.py" in publish
  4. get_redis_cli().publish(channel, json.dumps(message)) File "/usr/lib/python2.7/json/init.py" in dumps
  5. return _default_encoder.encode(obj) File "/usr/lib/python2.7/json/encoder.py" in encode
  6. chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python2.7/json/encoder.py" in iterencode
  7. return _iterencode(o, 0) File "/usr/lib/python2.7/json/encoder.py" in default
  8. raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /admin/mapping/tile/5/ Exception Value: <FieldFile: sampleapp/img5_L3sBxJI.png> is not JSON serializable

The problem seems to be in https://github.com/jonashagstedt/swampdragon/blob/master/swampdragon/serializers/field_serializers.py#L34 we're checking if type of of the 'value' is a FileField, but it's actually a FieldFile as described in the traceback above and explained at https://docs.djangoproject.com/en/1.8/ref/models/fields/#filefield-and-fieldfile.

Hence, I think we should be using FileSerializer in the case where value is a FieldFile as well.