mirror of https://github.com/scrapy/scrapy.git
support FEED_EXPORT_FIELDS=[]
This commit is contained in:
parent
c412034864
commit
9fb318338b
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue