Skip to content
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

3.15 is raising required error on model nullable fields #9378

Open
Enorio opened this issue Apr 11, 2024 · 6 comments
Open

3.15 is raising required error on model nullable fields #9378

Enorio opened this issue Apr 11, 2024 · 6 comments

Comments

@Enorio
Copy link

Enorio commented Apr 11, 2024

I have a model with nullable fields. Upgrading from 3.14 to 3.15 I'm getting this error:
AssertionError: You cannot call .save() on a serializer with invalid data.

When I print the serializer.errors after serializer.is_valid(), it says those fields are required. Is it possible that this is a bug, or do I have to manually override the field in the serializer to have the required=False kwarg?

@sevdog
Copy link
Contributor

sevdog commented Apr 23, 2024

Could you provide a code sample of your model, serializer and data for checking?

@Enorio
Copy link
Author

Enorio commented May 21, 2024

class Foo(...):
    ...
    value = models.IntegerField(null=True)

class FooSerializer(serializers.ModelSerializer):
    class Meta:
        model = Foo
        fields = '__all__'


serializer = FooSerializer(data=data) # This data does not have the "value" field
serializer.is_valid()
saved_data = serializer.save()

Raises this error AssertionError: You cannot call .save() on a serializer with invalid data.

@sevdog
Copy link
Contributor

sevdog commented May 22, 2024

The value field does not provide any default, so it is expected to fail if no value for that field is provided.

The real issue would be how did that ever worked without a default in the first place?
It should have worked if you had default=None or blank=True on your model's field (but I am not confident in the latter alone would work).

Usually a nullable field which is not required in forms is defined as:

class Foo(...):
    ...
    value = models.IntegerField(null=True, default=None, blank=True)

@Enorio
Copy link
Author

Enorio commented May 22, 2024

I'm using a django v3.y.z for now, don't know if that's the problem.
I have a lot of values with null=True without the default kwarg and never had a problem in the serializer. My guess is if you have only null, this counts as default=None?

@sevdog
Copy link
Contributor

sevdog commented May 22, 2024

My guess is if you have only null, this counts as default=None?

It is not documented nor in the codebase of django such behaviour for null or default. Django and the majority of DBMS does not suppose the default based on the assumption that a field is nullable.

I can only guess that there is something else which is providing a value for those fields in your project (a middleware? the frontend? custom views? custom serializers? custom base models?).

@Enorio
Copy link
Author

Enorio commented May 22, 2024

I don't think so, what I have it's pretty standard.
I'll look better into it and check the differences between these 2 DRF versions.

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

2 participants