Skip to content

Model defaults ignored on empty text field or empty boolean field. #3788

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

Closed
a1Gupta opened this issue Jan 1, 2016 · 14 comments
Closed

Model defaults ignored on empty text field or empty boolean field. #3788

a1Gupta opened this issue Jan 1, 2016 · 14 comments

Comments

@a1Gupta
Copy link

a1Gupta commented Jan 1, 2016

I am using Django 1.8 and DRF 3.3.1. I am still facing similar issue referenced in #1101.

class UserSetting(models.Model):
    allow_search_by_email = models.BooleanField(default=True)
    allow_profile_indexing = models.BooleanField(('Allow indexing?'), default=True)

class UserSettingSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserPrivacySetting
        fields = [
                    'allow_profile_indexing','allow_search_by_email',
                    ]

But whenever I am posting a blank form, and validating the serializer, it sets false to both the fields.

The issue #1101 was closed in #2311 but I am still facing the similar issue.

Is it fixed ? or
Should i use the workaround using extra_kwargs ?

@xordoquy
Copy link
Collaborator

xordoquy commented Jan 1, 2016

This should be fixed as we have a passing test case.

@a1Gupta
Copy link
Author

a1Gupta commented Jan 4, 2016

That means it should take model defaults if field is blank ?

@xordoquy
Copy link
Collaborator

xordoquy commented Jan 7, 2016

Somehow there seems to be differences between your use case and the DRF test cases. This difference needs to be highlighted.

@witte-de-with
Copy link

@xordoquy When you say this should be fixed, do you mean in an upcoming release? I'm on the latest DRF (3.3.2) and latest Django (1.9) and am still seeing this problem: default values for BooleanFields are not working as expected -- but only in the context of unit tests (things work fine via normal access to the API). I was able to use the extra_kwargs work-around to set the default there, and that fixed the problem I was running into with my tests, but am hoping this might be fixed in the framework.

Thanks for all the hard work on DRF. It's a lot of fun to use and the docs are great!

@xordoquy
Copy link
Collaborator

@i-s-o-g-r-a-m #2311 was part of 3.0.3 so it should work with 3.3.2
I'd be really interested in a failing test case or code sample we could look at to understand what's going on.

@witte-de-with
Copy link

@xordoquy I spent some time with a fresh Django/DRF install (latest stuff from pip as of 7 Feb 2016) and wasn't able to repro the problem. It's weird. I'm gonna go back to the codebase where I ran into this problem initially to see if I can extract something from there. I'm starting to wonder if the discrepancy might be more occult -- maybe even the backend (I noticed the problem with Postgres, whereas my attempt today was using SQLite).

@tomchristie
Copy link
Member

and wasn't able to repro the problem.

Okay, thanks! Closing this until/unless we have a reproducible case.

@kgodey
Copy link

kgodey commented Jun 2, 2016

I ran into this today using DRF 3.3.3 / Django 1.9.6 and Postgres. My Location model has a boolean field called is_active with default True. The frontend does not send this field to the API.

When I include is_active in the fields option of the serializer, it's ignoring the model default and getting saved as False. When I remove it from the fields option, it's saving correctly as True.

@tomchristie
Copy link
Member

Any chance we could get this reduced to a simple example case?
A description of the serializer, and the request as sent by a tool such as curl would be a good point for use to look into reproducing the issue.

@flaiming
Copy link

flaiming commented Jun 23, 2016

I added a simple failing test example for BooleanField. I think the root cause is that serializer is behaving differently when you initialize it with request.data (which is QueryDict) or just simple dict. Turning point is here: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L384

@flaiming
Copy link

Could this ticket be reopened please, since we now have failing test code sample?

@xordoquy
Copy link
Collaborator

Did you made a PR for that failing test case ? We need to investigate it before taking further action

@tomchristie
Copy link
Member

Keeping this closed, as per note in #4284.

@resurrexi
Copy link

I was encountering this issue when running my test cases using the APIClient.

It turns out that if I send my data as a multipart form request, the test case will fail, but if I specify format='json' or configure my settings 'TEST_REQUEST_DEFAULT_FORMAT': 'json', my test case will pass.

It might be worth mentioning it in the testing docs.

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

7 participants