Hi Django developers community
A great greetings
I want share with you the following question:
I have a custom users schema in Django for work with roles or users type,
creating an application named userprofile which will be or will setup my
custom user model.
In my settings.py I have the following configuration:
INSTALLED_APPS = [
...
'userprofile',]#Custom model Users
AUTH_USER_MODEL = 'userprofile.User'
I customize my User class (userprofile/models.py) that inherit of the
AbstractUser class for add some fields to my User model due to my
requirements demanded me.
I also create these another models for roles/profile users (MedicalProfile,
PatientProfile, PhysiotherapistProfile) with their own fields or attributes
In addition MedicalProfile, PatientProfile, PhysiotherapistProfile have a
OneToOneField relationship with my custom model/class User
All this I want denote in this figure userprofile/models.py
<https://lh3.googleusercontent.com/--0TVa2mFIIs/VoRWT41c9kI/AAAAAAAADHo/Qi9t2mn_sWA/s1600/UserRolesModels.png>
*My Question*
I want to focus my question in relation about of the post_save signal
operation, this mean in the create_profile_for_new_user() method:
@receiver(post_save, sender=settings.AUTH_USER_MODEL)def
create_profile_for_new_user(sender, instance, created, **kwargs):
user = instance
# ----------------- Begin debug----------------------
import ipdb
#------------------------
if created:
if user.is_medical:
ipdb.set_trace()
profile=MedicalProfile(user=instance)
profile.save()
I want, each that an user is created, automatically (by post_save signal
action) be created their profile (MedicalProfile, PatientProfile,
PhysiotherapistProfile) according to if their field checked (is_medical,
is_patient, is_physiotherapist) at the User class
<https://cloud.githubusercontent.com/assets/1211088/12054842/f7662f4c-aef4-11e5-86ba-23e64dcf83c6.png>
(same
black image presented above)
The inconvenient that I have is with my signal, and it's the following:
- When I create an user via django admin, indicating that this user is
medical (have the is_medical field/attribute checked in the user admin
form) in my MedicalProfile model, their profile instance not is saved or
don't created
I have been checking the situation through ipdb basic debug functions and
I think so that when I create the user the compiler or the steps don't
arrive to
the section when I check if my user is medical (if user.is_medical code
line)
such as follow:
1. I've placed a debug point initially of this way:
<http://i.stack.imgur.com/MRwUN.png>
1. I create a user via django admin
I go to my Django admin and I create a new user and when press click in
"Save" (first moment to user create):
<http://i.stack.imgur.com/IsxBB.png>
*3. When I press "Save" button, the debug point enter to action*
Then, I check the value of the variables that accord to my function which I
am applying the signal are defined until the moment and they are inside the
debug process. They are:
- sender, created, instance (this mean, the parameters of the function)
- user, that is the same instance variable
<http://i.stack.imgur.com/8smfA.png>
The above, denote that the parameters are arriving. All is O.K for the
moment.
1. *4. Then, I perform the debug a few more below in the code (red
square in the following figure) and I analyze the following:*
<http://i.stack.imgur.com/YDJ9y.png>
It's true the sender, instance and created parameters are arriving and when
check for the is_medical attribute (boolean) which depend that the profile
be saved in MedicalProfile model, I get this:
<http://i.stack.imgur.com/HeKHz.png>
My attribute is_medical have the boolean value as False and I think that I
can understand it,
because we know that the user creation is divided in two sequentials phases
in the django admin
- The first phase when I allocate username and password
<http://i.stack.imgur.com/XwVV5.png>
- And the second phase when I enter the other fields or attributes in
the creation of the that same user.
<http://i.stack.imgur.com/djQiz.png>
I think that when the user is saved in the first phase (I allocate only
username and password) the is_medical checkbox not yet is setup (checked)
and this is the reason by which their value is False
It's right?
1. *5*. If I placed a few more below the set_trace() point debug such as
follow in the figure (red square):
<http://i.stack.imgur.com/cBuCI.png>
In this point, ipdb do not enter in action, my debug is not reached or does
not work.
The signal action that is doing is create the new User, but not create the
profile for the user, in this case, the MedicalProfile beacuse I checked
the is_medical checkbox when I was create the user. Just create the user
and nothing more ...
I think that the is_medical attribute never is True, despite that I checked
via admin django form, ant these it's the condition for that my user
instance be saved in MedicalProfile model. :(
What alternatives can I have for solve this situation and when I create an
user, their profile instance be saved (MedicalProfile,
PhysiotherapistProfile, PatientProfile) depending of the attribute
checkbo/field (is_medical, is_physiotherapist , is_patient) that I choose?
I bring to all my apologies before, in case of the my question do not be
suited or appropriated with the group philosophy or by the extense of my
question.
The reason that it's extense is that I want give all details for get an
answer
Any orientation I will be grateful and will be appreciated
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/700358cf-f36f-44ea-9c17-cd4c84ac627c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.