Skip to content

Commit

Permalink
Merge branch 'denhartog-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Feb 3, 2018
2 parents 0b8ddfe + c003c0d commit b8cd3c6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Here's how to create a script to import CSV data into the model. Our favorite wa
def handle(self, *args, **kwargs):
# Since the CSV headers match the model fields,
# you only need to provide the file's path
Person.objects.from_csv('/path/to/my/import.csv')
insert_count = Person.objects.from_csv('/path/to/my/import.csv')
print "{} records inserted".format(insert_count)
Run your loader.

Expand Down Expand Up @@ -157,7 +158,7 @@ And so will something like this:
Import options
==============

The ``from_csv`` manager method has the following arguments and keywords options.
The ``from_csv`` manager method has the following arguments and keywords options. Returns the number of records added.

.. method:: from_csv(csv_path[, mapping=None, drop_constraints=True, drop_indexes=True, using=None, delimiter=',', null=None, force_not_null=None, force_null=None, encoding=None, static_mapping=None])

Expand Down
2 changes: 2 additions & 0 deletions postgres_copy/copy_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def save(self, silent=False, stream=sys.stdout):
if not silent:
stream.write("{} records loaded\n".format(intcomma(insert_count)))

return insert_count

def get_field(self, name):
"""
Returns any fields on the database model matching the provided name.
Expand Down
4 changes: 3 additions & 1 deletion postgres_copy/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ def from_csv(self, csv_path, mapping=None, drop_constraints=True, drop_indexes=T
if drop_indexes:
self.drop_indexes()

mapping.save(silent=silent)
insert_count = mapping.save(silent=silent)

if drop_constraints:
self.restore_constraints()
if drop_indexes:
self.restore_indexes()

return insert_count

def to_csv(self, csv_path, *fields, **kwargs):
"""
Copy current QuerySet to CSV at provided path.
Expand Down
3 changes: 2 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def test_limited_fields(self):
)

def test_simple_save(self):
MockObject.objects.from_csv(
insert_count = MockObject.objects.from_csv(
self.name_path,
dict(name='NAME', number='NUMBER', dt='DATE')
)
Expand All @@ -245,6 +245,7 @@ def test_simple_save(self):
MockObject.objects.get(name='BEN').dt,
date(2012, 1, 1)
)
self.assertEqual(insert_count, 3)

def test_loud_save(self):
MockObject.objects.from_csv(
Expand Down

0 comments on commit b8cd3c6

Please sign in to comment.