Skip to content

3.15 is raising required error on model blank fields #9750

@sshishov

Description

@sshishov

This issue is very similar to #9378 but the problem is happening with the CharField(blank=True,...).

It is not recommended to use null=True with CharField because of double empty values like None and '', therefore we are using the blank=True to mark the field not required (kind of).

Now we have the following constraint on the model:

class MyClass(django_models.Model):
    ...
    fk_field = django_models.ForeignKey(to='myapp.OtherModel', on_delete=django_models.CASCADE, related_name='myclasses')
    char_field = django_models.CharField(max_length=255, blank=True)
    ...
    class Meta:
        constraints = [
            django_models.UniqueConstraint(
                fields=['fk_field', 'char_field'], condition=~django_models.Q(char_field=''), name='unique_fk_field_%(class)s_char_field'
            )
        ]

This setup is causing issues with ModelSerializer (without overriding extra_kwargs) for char_field as it becomes required.

The current setup explicitly allow '' (empty string) as default to be excluded from unique constraint. What is the approach to handle it? Should we manually add this using extra_kwargs or we can solve it the same way how #9378 was solved?

We can add something like:

if unique_constraint_field.blank:
    default = ''

Am I missing something here?

NOTE: this is kind of a blocker for us to move forward from 3.14. If the decision will be to support it manually, then we have to update around 10 different places... or we can wait for the fix from library...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions