Skip to content

Conversation

ilkkao
Copy link

@ilkkao ilkkao commented Mar 21, 2025

I'm not sure at all if this my mistake or a real issue. But thought it's easiest to explain it as a PR.

Without this fix, in my django app, a string [] gets saved to the phone_number model view when nothing is entered to the form SplitPhoneNumberField. Model field is defined as:

    phone_number = PhoneNumberField(blank=True, null=True)

Field in my form:

    phone_number = AdminSplitPhoneNumberField(
        required=False,
        help_text='Phone number, country code separated'
    )

In my view I just call

self.form_valid(my_form)

@francoisfreitag
Copy link
Collaborator

I've seen your message only recently, I'll investigate this issue.

@francoisfreitag francoisfreitag force-pushed the ilkkao/split-field-update branch from 97f6c63 to 1c180a3 Compare July 4, 2025 20:21
@francoisfreitag francoisfreitag changed the title Convert empty list to None before saving to db Allow configuring empty_value for SplitPhoneNumberField Jul 4, 2025
@francoisfreitag francoisfreitag force-pushed the ilkkao/split-field-update branch from 1c180a3 to c3a0973 Compare July 4, 2025 20:23
@francoisfreitag
Copy link
Collaborator

Investigated the issue. From the commit message:

Previous behavior was to return an empty list for no data, which is surprising and cannot be saved to a django.db.models.CharField in the database.
Following django.forms.CharField behavior, accepts an empty_value, defaulting to the empty string.

@ilkkao, when you have the chance, could you try out this branch in your project and confirm it solves your issue?

Since your field is nullable, you might need to subclass the SplitPhoneNumberField to specify empty_value=None. Django has some logic to set the empty value to None when the field is nullable, so it might just work out of the box, depending on your project.
https://github.com/django/django/blob/6df19412aabb7d969f5eab4b2ff41269de89b233/django/db/models/fields/__init__.py#L1308

@ilkkao
Copy link
Author

ilkkao commented Jul 4, 2025

I'll add a reminder for myself to comment in couple weeks. I'm travelling currently.

@francoisfreitag
Copy link
Collaborator

Thanks!
I noticed the SplitPhoneNumberField is incompatible for use with formfield for the model fields, because the model field specifies a max_length and the form field does not know of it. I’m working on adding max_length to the SplitPhoneNumberField.

@francoisfreitag
Copy link
Collaborator

I believe the PR is ready for testing.
I ended up making the empty value configurable, so that users can choose between '' and NULL for storing an empty value in the DB.

After that, I realized it wasn't enough to use the field with fields_for_model, so I added support for the max_length attribute.

ilkkao and others added 2 commits August 22, 2025 09:08
Previous behavior was to return an empty list for no data, which is
surprising and cannot be saved to a django.db.models.CharField in the
database.

Following django.forms.CharField behavior, accepts an empty_value,
defaulting to the empty string.
Allow using the field with `fields_for_model`, `ModelForm`, …
@francoisfreitag francoisfreitag force-pushed the ilkkao/split-field-update branch from 045d6b5 to 76da570 Compare August 22, 2025 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants