palewire / django-postgres-copy

Quickly import and export delimited data with Django support for PostgreSQL's COPY command
https://palewi.re/docs/django-postgres-copy/
MIT License
179 stars 48 forks source link

Model-method transformation doesn't work after mapping #120

Open gabbork opened 4 years ago

gabbork commented 4 years ago

Maybe putting a little abstract of the code will explain better what is happening: file.csv

ColumnName
"144,54"
"435,12"

models.py

MyModel(models.Model):
    my_attr_name = models.DecimalField(max_digits=10, decimal_places=2)
    objects = CopyManager()

    def copy_ColumnName_template(self):
        return """
            replace(%(name)s::text, ',', '.')::numeric * 1000
        """

management command

insert_count = MyModel.objects.from_csv(
    csv_path,
    mapping={"my_attr_name": "ColumnName"}
)

When I execute the command, I get a django.db.utils.DataError: invalid input syntax for type numeric: "144,54". It happens the same on any field typology, it's like the copy_ColumnName_template is skipped.

Am I missing something or using the transformation in a bad way? Without mapping, everything works as expected. Thanks