-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Model defaults ignored on empty text field or empty boolean field. #1101
Comments
this problem have in tests and when open auto generic api web, in section raw data but if send post request via curl or another...everything is working properly, is_active set TRUE |
This problem hasn't fixed even in 2.3.9. |
Is there any open pull request associated with this issue? |
I have never submitted a patch or anything before, but would this be a suitable failing test case? Would it work with /tests/test_serializer.py?
|
Hello, I encountered this bug when upgrading from version 2.3.9 to 2.3.12 of django-rest-framework, and eventually narrowed it down to this commit (90edcbf). I think this fix doesn't quite address what the actual issue is: the issue is that if you create an object using the HTML form and leave a checkbox unchecked, it will leave out that value from the form, and that should be interpreted as indicating that the value should be false, regardless of its default; whereas if you create an object using some other format (JSON or whatever) and leave out a value, that means the value should be its default. In other words, the same data needs to be treated differently depending on whether it came from an HTML form or not. So I think this means that either the HTML form needs to be changed to explicitly represent an unchecked checkbox as meaning false (which I'm not sure whether is possible), or the serializer needs to be changed to check whether its data came from an HTML form or not (which I'm also not sure whether is possible, but maybe it could be done in some pre-serializer step?). Is this a correct assessment? I'm going to put in a fix on my end for the specific case I have, and if I have time I'll try to figure out how to fix it within django-rest-framework. But if someone who's familiar with the code knows of any obvious fixes, that would be great. Avril |
That all sounds sensible, yeah.
Not sure. The acid test is - can this be translated into a test case that clearly demonstrates non-expected behaviour? |
So, I'm not sure exactly what behavior is expected/correct, because I don't know all the use cases. It sounds like the person who originally opened this issue was expecting that unspecified values will always get their default value, but from my point of view that behavior is wrong, because then there is no way to indicate in an HTML form request that you want a value to be false when its default is true. (Does django-rest-framework allow partial-updates to be submitted using HTML forms? That would have the same problem of not being able to specify that a boolean should be false.) I found this solution on the internet to the problem of forms leaving out unchecked checkboxes: http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/. But it's a frontend solution, which means it doesn't solve the problem if someone submits form data from some other frontend. In 90edcbf#diff-af402f515db75c7c6754453cf15455d9L431, does the data's being a QueryDict indicate that it definitely came from a form? Because if that is the case, then I think it should be the way that it was before that commit, where unspecified means false. |
The main problem of this issue was not honouring default values specified in models when we run Django's test runner for partial update/create, which was apparently a bug so that had to be fixed. |
I think im seeing another instance of this. The django test client and testing by hand using runserver (or any web interface) yields different results. EG.
That final test shouldn't fail, and doesn't testing using a web client. The response from the post has a 200 status. |
#1291 doesn't solve the problem, actually @tomwalker 's solution is targeting for the root cause (horouring model default value). I would suggest reopen PR #1248, get the test pass, and merge the fix. Anybody know how to run the rest_framework test? |
@caogecym it's explained in the documentation. See http://www.django-rest-framework.org/topics/contributing#testing |
This bug is happening again in version 3.0.1... :( |
Assuming same as #2280? |
Unfortunately it was not. |
Retitling with what I think may be a more specific wording for the behavior you're seeing. |
Apologies for the slightly higher than usual churn rate, due to 3.0 just being released - pretty much all settled down now, but this is one of the few remaining outstanding issues. |
Fix in the meantime, add the following to the serializer to forcibly set the default values...
|
@tomchristie Thanks for the swift reply. Yes, the temporal workaround did the trick. |
Issue here: When a model has a default we simply set Answer is probably that |
encode/django-rest-framework#1101 Signed-off-by: Dilvane Zanardine <[email protected]>
I found a bug in the coreapi to create a domain this problem is on the djangorestframework and fix ten days ago see: mozilla/mozillians#1892 encode/django-rest-framework#1101 Fix: https://github.com/encode/django-rest-framework/pull/5038/files
djangorestframework==3.7.1
Notice, not sending the field 'open' Had to this to get my test to pass - Item.open = True by default class Meta: |
I found a bug in the coreapi to create a domain this problem is on the djangorestframework and fix ten days ago see: mozilla/mozillians#1892 encode/django-rest-framework#1101 Fix: https://github.com/encode/django-rest-framework/pull/5038/files
djangorestframework>=3.10.1,<3.11.0 Encountered the same problem. |
I have the same problem on version 3.11.0 |
that's the way boolean field send by HTML work. Specification says that if it's not set it should be considered as |
Is there any workaround? Why can you define the default value of Even when setting |
Because it works with anything except html forms.
Sure and the cool thing is it's already documented |
This still happens to me (3.12.1). httpie sending no data results in a True value, self.client.post sending no data results in False. |
@amikrop which is consistent with previous comments. httpie sending json by default as opposed to self.client.post. |
That's right, adding |
My tables has boolean fields like below.
and save it using a serializer in a view class without passing the enabled field in the request.
the result is
object.enabled == False
.I've updated the framework to version 2.3.8 then the issue started occurring. Everything works fine in 2.3.7.
Affected versions >= 2.3.8
The text was updated successfully, but these errors were encountered: