Originally published on my website
In the first part of the intro django rest framework, we set up the project and did an overview of what the pro...
For further actions, you may consider blocking this person and/or reporting abuse
Hi! Nice post, was really useful to me. But now I want to expand on this. I have different models that I want to relate to a User of the system (lets say, Student and Teacher), each with different fields. How can I differentiate the registration of these?
hey Fmesteban,
really happy you found this post helpful.
One way you can approach this is by extending the Django AbstractUser model and introducing a field by the name role.
From this, you can make different UserProfiles like indicated from the post. For instance:
After that has been set up, you'll just make a post_save signal that listens to the type of role on the new UserAccount instance. If it's a teacher, initialize TeacherProfile instance and populate it.
This can be achieved in a multistep form. Same for the student profile.
Thanks for the quick response! I had about the same idea but wanted to make sure that was the way to go.
Thanks again!
You're welcome :)
sir i want to use this role choice in the login process hwo can i do ti ?
How can I use raw json as POST data instead of
x-www-form-urlencoded
?Hey, Sagar, you can use this method on postman;
or if you want to use curl,
in both instances, the user is created as you can see below,
I got the error
NameError: name 'APITestCase' is not defined
when I ranpython manage.py test
. No surprises there as that was not imported or earlier defined. How is this resolved?Hello Olaniyi,
Are you working on your own implementation of the code?
If so kindly share the GitHub link and we can debug together. Mine is working ok as far as I can tell.
Here's a screenshot of the same.
Here's the code from the tests.py file.
Hey great posting, I'm trying to achieve the exact same thing you are doing but I'm not sure how to create a new profile. What I want to do is call the POST /auth/users method with additional fields so that it creates the profile and the user at the same time. Is this realistic? Or I need to perform 2 steps (i.e. one call to create the user and a second call to create the profile)
Thanks in advance!
Hey. Yes it would.
The best way to add extra fields is to extend django user model with AbstractUser, then place those extra fields in the REQUIRED_FIELDS array.
That way the djoser /auth/users/ endpoints can recognize the additional fields on the CustomUser model.
I had initially made the user profile to demonstrate signals to the readers :)
You can use django's AbstractUser Class to extend the user model. Here's the documentation
Thank you for sharing! It works like a charm!! So helpful!
Welcome! Glad it helped :)
very useful tutorial , can you put information about reset-password ,activation, ...
hey, yes I will. Working on a full-stack tutorial with Django and vue.js. it'll be covered there
how to create a logoutView i try blacklist but not work, my refresh token is always still valid
Thank you so much !
Thank you for this!
welcome ;)
I cannot find the words to describe what I explained. Thank you very much, I am waiting for what you will offer in the next explanations about DJANGO REST <3
thank you. Comments like yours encourage me to write more and better. I'll be continuing with this next week. Had taken a small writing break ;)
Thank you for this!
welcome. If you have any questions, feel free to contact me anytime.
in the test case there is no user data
Hi, nice post! it's very helpfull.
But using djoser and creating the user endpoints would make 2 endpoints for retrieving and listing users, wouldn't it? is this a problem?
Hey. Yes it would.
The best way to add extra fields is to extend django user model with AbstractUser, then place those extra fields in the REQUIRED_FIELDS array.
That way the djoser /auth/users/ endpoints can recognize the additional fields on the CustomUser model.
I had initially made the user profile to demonstrate signals to the readers :)