support FEED_EXPORT_FIELDS=[]

This commit is contained in:
Mikhail Korobov 2015-05-13 00:33:48 +05:00
parent c412034864
commit 9fb318338b
3 changed files with 19 additions and 7 deletions

View File

@ -236,17 +236,20 @@ The serialization format to be used for the feed. See
FEED_EXPORT_FIELDS
------------------
Default: ``None``
A list of fields to export, optional.
Example: ``FEED_EXPORT_FIELDS = ["foo", "bar", "baz"]``.
Use FEED_EXPORT_FIELDS option to define fields to export and their order.
When omitted or empty, Scrapy uses fields defined in :class:`~.Item` subclasses
a spider is yielding. If raw dicts are used as items, FEED_EXPORT_FIELDS
is omitted and an exporter requires a fixed set of fields (this is the case
for :ref:`CSV <topics-feed-format-csv>` export format) then Scrapy tries
to infer field names from the exported data - currently it uses field names
from the first item.
When FEED_EXPORT_FIELDS is None (default), Scrapy uses fields
defined in :class:`~.Item` subclasses a spider is yielding.
If FEED_EXPORT_FIELDS is None, raw dicts are used as items and
an exporter requires a fixed set of fields (this is the case for
:ref:`CSV <topics-feed-format-csv>` export format), then
Scrapy tries to infer field names from the exported data - currently it
uses field names from the first item.
.. setting:: FEED_STORE_EMPTY

View File

@ -151,7 +151,12 @@ class FeedExporter(object):
if not self._exporter_supported(self.format):
raise NotConfigured
self.store_empty = settings.getbool('FEED_STORE_EMPTY')
self.export_fields = settings.getlist('FEED_EXPORT_FIELDS') or None
if settings.get('FEED_EXPORT_FIELDS') is None:
self.export_fields = None # don't promote None to []
else:
self.export_fields = settings.getlist('FEED_EXPORT_FIELDS')
uripar = settings['FEED_URI_PARAMS']
self._uripar = load_object(uripar) if uripar else lambda x, y: None

View File

@ -246,6 +246,10 @@ class FeedExportTest(unittest.TestCase):
yield self.assertExported(items, header, rows,
settings=settings, ordered=True)
# edge case: FEED_EXPORT_FIELDS==[] means nothing is exported
settings = {'FEED_EXPORT_FIELDS': []}
yield self.assertExportedJsonLines(items, [{},{},{},{}], settings)
@defer.inlineCallbacks
def test_export_dicts(self):
# When dicts are used, only keys from the first row are used as