etianen/django-reversion — Issue #109: Setting fields to be saved (fields option)
Author: demalexx
Created: 2011-11-28 06:59:04+00:00
State: open

Hello,

I wonder if this setting (along with `exclude`) is working for someone. The problem is `VersionAdapter.get_fields_to_serialize` is a generator. Later this generator is used in `django.core.serializers.base.py`, `serialize` function to check if current field is in a list of fields to serialize. But generators are different than lists/tuples and operator `in` works differently:

```
>>> g = (x for x in range(1, 3)); 1 in g; 2 in g;
True
True

>>> g = (x for x in range(1, 3)); 1 in g; 1 in g;
True
False

>>> g = (x for x in range(1, 3)); 2 in g; 1 in g;
True
False

g = (x for x in range(2, 3)); 1 in g; 2 in g;
False
False
```

So, `VersionAdapter.get_fields_to_serialize` as generator will work only if it'll have all fields and in order `serialize` iterates them.

The solution could be simple: `VersionAdapter.get_serialized_data` function instead of using `get_fields_to_serialize` as generator will convert it to list:

```python
def get_serialized_data(self, obj):
"""Returns a string of serialized data for the given obj."""
return serializers.serialize(
self.get_serialization_format(),
(obj,),
fields = list(self.get_fields_to_serialize()), # here we convert generator to a list
)
```

Am I doing something wrong, or is it library bug?