Some CSV Utilities for your django projects
pip install django-csv
from django_csv import CSVUtilities
- Create a new
CSVUtilities
instance with a single parameter, the path to a CSV file
read_csv_into_model(model, csv_indices, delimeter=',', filter_fun=None, after_creation=None, start_row=0, dry_run=False, init_args={})
Create django model instances from the rows of this CSVUtilities
instance's csv file. Each row of the csv will map to a new model instance unless the row doesn't pass the filter_fun
test.
Arguments:
model
- the django class to create instances ofcsv_indices
- a dictionary of field names mapped to the column index in the csv which holds the field valuedelimiter
- the csv delimiterfilter_fun
- a callable which receives the current row in the csv as it's single argument. ReturnTrue
for the row to create a new model instance andFalse
for the row to be skipped. If not provided, will never skip rows.after_creation
- a callable which receives two arguments, the (unsaved) model instance and the current csv row. Ifafter_creation
is provided you are responsible for callingsave()
to write the instance to the db.start_row
- the row in the csv to start reading fromdry_run
- ifTrue
, no model instances will be saved, just the count of the number of model instances that would be created will be returned.init_args
- dictionary of attributes to set for every created model instance
write_model_to_csv(model, csv_file=None, delimiter=',', header_row=False, qs=None)
Write django model instances to either the csv file at this CSVUtilities
instance's path or the path given by csv_file
if it is given.
Arguments:
model
- the django class to read instances fromcsv_file
- if given, this is the path to the csv file to write. If not given, we use the path in thisCSVUtilities
instance.delimiter
- the csv delimiterheader_row
- if True, the first row in the written csv file will be the model field namesqs
- a QuerySet that - if passed - will be used in place ofmodel.objects.all()
as the data source for the csv file. The models ofqs
should be of the same type asmodel
for data consistency.
check_pred_against_rows(pred, delimiter=',')
Get a list of all csv rows that pass the given predicate
Arguments:
pred
- a callable which receives the current csv row as its single parameter and should return a truthy valuedelimiter
- the csv delimiter
csv_from_excel(excel_file, csv_name)
Create a csv file from an excel file.
Note: Requires xlrd
package.
Arguments:
excel_file
- a File object which we'll create the csv file fromcsv_name
- path to the csv file to create (shouldn't include the .csv extension)
Base Class:
class DjangoClass(models.Model):
name = models.CharField(max_length=200)
age = models.IntegerField()
CSV File (filename: 'ppl.csv'):
1,Joe Schmo,I like cars,22
2,Bob Stein,I like turtles,24
3,Mary Jane,I like spiders,35
Basic example of reading into model:
import django_csv
csv_utils = django_csv.CSVUtilities('ppl.csv')
csv_utils.read_csv_into_model(DjangoClass, {
'name': 1,
'age': 3
})
Basic example of creating csv from excel file:
from django_csv import CSVUtilities
def some_view(request):
CSVUtilities.csv_from_excel(request.FILES.get('excel_file'), 'csvs/some/csv')
Basic example of writing model instances to csv file:
from django_csv import CSVUtilities
def some_view(request):
csv_utils = django_csv.CSVUtilities('ppl.csv')
csv_utils.write_model_to_csv(DjangoClass)
More advanced (and admittedly convoluted) example of reading into model:
import django_csv
class Description(models.Model):
text = models.TextField()
class DjangoClass(models.Model):
name = models.CharField(max_length=200)
age = models.IntegerField()
description = models.ForeignKey(Description)
@staticmethod
def _after_create(instance, row):
description = Description.objects.create(text = row[2])
instance.description = description
instance.save()
@classmethod
def create_from_csv(cls):
csv = 'ppl.csv'
csv_utils = django_csv.CSVUtilities(csv)
csv_utils.read_csv_into_model(DjangoClass, {
'name': 1,
'age': 3
}, after_creation=cls._after_create)