M1ha-Shvn / django-pg-bulk-update

Django manager for performing bulk update operations in PostgreSQL database
BSD 3-Clause "New" or "Revised" License
39 stars 13 forks source link

Twice value format before saving #51

Closed kids300 closed 4 years ago

kids300 commented 4 years ago

In django_pg_bulk_update/utils.py(108)format_field_value() value is formated twice: https://github.com/M1hacka/django-pg-bulk-update/blob/5540d1125c9972629fdc08611af36f3eb85fa1fa/src/django_pg_bulk_update/utils.py#L75 and https://github.com/M1hacka/django-pg-bulk-update/blob/5540d1125c9972629fdc08611af36f3eb85fa1fa/src/django_pg_bulk_update/utils.py#L108 I implement a field where the value can only be formatted once before saving (divide by 100):

class Divide100Field(models.FloatField):
    def to_python(self, value):
        value = super().to_python(value)
        if not value:
            return value
        value /= 100
        return value

    def get_prep_value(self, value):
        try:
            value = super().get_prep_value(value)
        except ValueError as e:
            if not value:
                return None
            raise e

        return self.to_python(value)

Can we change the format_field_value behaviour?

M1ha-Shvn commented 4 years ago

Hi To be the truth, it looks like a bug in format_field_value. From the first view, it should be one big if condition, not two separate. This function has been pasted from https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L1426 when this library has been created (if I'm not mistaken, on django 1.10) and modified a little bit. Nowadays it's not easy to tell if this bug has been pasted or made here, as source changed greatly. I'll test it out on weekend and change if possible.

M1ha-Shvn commented 4 years ago

Released 3.1.1

kids300 commented 4 years ago

Great, thx!