Skip to content

Option to exclude Django Field blank option from determination of 'nullable' #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tomatod opened this issue Apr 13, 2025 · 1 comment

Comments

@tomatod
Copy link

tomatod commented Apr 13, 2025

Is your feature request related to a problem? Please describe.
When blank=True is specified at a Djang Field, ModelSchema treats the field as nullable even if null=False is also specified there. So, ModelSchema passes null to the Django model and it raise NOT NULL constraint exception.

class CharFieldVerification(models.Model):
    char_field = models.CharField(max_length=200, blank=True, null=False)

class TextFieldVerification(models.Model):
    text_field = models.TextField(blank=True, null=False)

class DateFieldVerification(models.Model):
    date_field = models.DateField(blank=True, null=False)

class FileFieldVerification(models.Model):
    file_field = models.FileField(blank=True, null=False)

class BooleanFieldVerification(models.Model):
    boolean_field = models.BooleanField(blank=True, null=False)

Image

The combination of blank=True and null=False seems to be common in string-based Django fields, which is mentioned in Django Model field reference

Other related Issue

@tomatod tomatod changed the title Some function to exclude Django Field blank option from nullable determination Option to exclude Django Field blank option from nullable determination Apr 13, 2025
@tomatod tomatod changed the title Option to exclude Django Field blank option from nullable determination Option to exclude Django Field blank option from determination of 'nullable' Apr 13, 2025
@tomatod
Copy link
Author

tomatod commented Apr 13, 2025

According to the Django reference, the blank option is mainly for form validation. Django is originally made for form applications and the target content-type is application/x-www-form-urlencoded, so I think Django field has the blank option because the content-type cannot express null explicitly.

Json can express null originally, so I think as an option it would be good if ModelSchema could simply look only at the null option.

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

No branches or pull requests

1 participant