Add include_fields() and exclude_fields() importer methods

for convenience, to tweak field list at runtime
This commit is contained in:
Lance Edgar 2019-02-14 10:34:29 -06:00
parent 8ae12ca1dc
commit f9bf01be5a

View file

@ -105,9 +105,7 @@ class Importer(object):
self.key = key
self.fields = fields or self.supported_fields
if exclude_fields:
for field in exclude_fields:
if field in self.fields:
self.fields.remove(field)
self.exclude_fields(*exclude_fields)
if isinstance(self.key, six.string_types):
self.key = (self.key,)
if self.key:
@ -120,6 +118,24 @@ class Importer(object):
self.model_name = self.model_class.__name__
self._setup(**kwargs)
def include_fields(self, *args):
"""
Add the given fields to the supported field list for the importer. May
be used at runtime to customize behavior.
"""
for field in args:
if field not in self.fields:
self.fields.append(field)
def exclude_fields(self, *args):
"""
Remove the given fields from the supported field list for the importer.
May be used at runtime to customize behavior.
"""
for field in args:
if field in self.fields:
self.fields.remove(field)
def get_model_class(self):
return self.model_class