iommirocks / iommi

Your first pick for a django power cord
http://iommi.rocks
BSD 3-Clause "New" or "Revised" License
718 stars 47 forks source link

parse_empty_string_as_none not working properly with choice #415

Closed berycz closed 1 year ago

berycz commented 1 year ago

I have CharField with choices, blank=True, null=False iommi Form by default renders the field as text-input, so I passed

        fields__foo__choices = ...

that makes it a select but on submit I get IntegrityError("null value in column "foo" of relation "bar_baz" violates not-null constraint")

the choices don't contain an empty choice, but I use Field__shortcuts__choice__empty_label = "Select an option" in style

I checked self.parse_empty_string_as_none in form.py Field._parse() and it is as it should be False but then self.parsed_data = self._parse_raw_value(self.raw_data) gets called and choice_parse returns None

for now I fixed it with

        @staticmethod
        def extra__pre_save(form, **kwargs):
            if form.instance.foo is None:
                form.instance.foo = ""