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

allow default boolean values to be honoured #1248

Closed
wants to merge 3 commits into from

Conversation

tomwalker
Copy link

In response to request #1101 "Default Boolean value always becomes False even default=True is specified in model class."

This allows a default to be used when the field is not specified in the request.

@@ -1720,3 +1720,36 @@ def test_missing_fields(self):
'b_renamed': None,
}
)
class DefaultTrueBooleanModel(models.Model):
cat = models.BooleanField(default=True)
dog = models.BooleanField(default=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not test both default=True and default=False?

@tomwalker
Copy link
Author

Whilst following the recommendations from Ian-Foote (thanks by the way) I found another small bug - if a default was set but the field was not specified when the serialiser was initialised then it would return as None.

This is seen with this:

serializer = self.default_true_boolean_serializer({'cat': False})

@tomchristie
Copy link
Member

Kick me on this thread if I've not reviewed this in the next few days :)

@kevin-brown
Copy link
Member

Just a few quick things that are a problem with this pull request.

When serializers are initialized, the first argument is the instance. So in all of your tests, the data is not being passed into the serializer, so none of them are valid (you can test this by calling serializer.is_valid(), it returns False). Once you fix that issue, the tests should continue to break. They were all failing because no data could be processed, which is also why your first test is invalid, as it won't do data processing without data.

The last test (test_enabled_partial) does not specify the partial parameter on initialization, so that also failed to validate.

With all of that in mind, I created a new pull request (#1291) which fixes the issue in a different way.

@tomchristie
Copy link
Member

Closing in favor of #1291. Thanks for the input!

@tomwalker
Copy link
Author

Hi Kevin,

Thank you for taking the time to explain everything - it has been a great help.

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.

4 participants