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
Django REST 3.8.2 CurrentUserDefault doesn't work #6031
Comments
Due to #5886 this will not work any longer. Somewhat off topic, but it looks like the documentation isn't up to date with this new behavior (http://www.django-rest-framework.org/api-guide/serializers/#specifying-read-only-fields)
|
The example there is still right for validation (where you do need the default due to the unique together constraint). We need to add a bit more re the need to supply the value when saving. |
Hi! I'm going to do some shameless self-promotion here (sorry for that), but my package should fix your issue. For your use-case of setting the current user (automagically), take a look at the Django-UserForeignKey Package. It works as a Django Middleware and sets the current user in your model, so you don't need to rely on your Devevelopers to always set the current user in your View Logic or serializers. (I've also created a YouTube Video for a demo). |
Another approach might be to set it in your serializer's |
@blueyed: Yes. Pass the value during Extra notes in the docs emphasising this would be welcome. Where would you (i.e. all of you) expect to see that note? (I don't think it'll need a lot. PRs there welcome.) |
@blueyed you code works like a magic!! Thank you! |
@damonsu @carltongibson Therefore it is likely more an issue when updating to the new behavior, and you have to remember this change (e.g. in the case of https://github.com/lock8/django-rest-framework-jwt-refresh-token I still was bisecting to find the issue, although I came across this before already). |
Indeed. 🙂 (IIRC, it was you that dug out the root issue here) |
Using |
When i tried to create a new Post, i get always the following error:
Exception Value: | (1048, "Column 'user_id' cannot be null")
But i get no error before i updated to Django Rest 3.8.2. It seems like that the function CurrentUserDefault doesn't work.
My Code:
**Solution based on the reply of @blueyed **
The text was updated successfully, but these errors were encountered: