Thursday, December 31, 2015

Strange behaviour while deleting related object

Hello All,

I have a model like this:

class Concept(models.Model):
    tag = models.ForeignKey(Tag, null=True, on_delete=models.SET_NULL)

Now, I've a piece of code in my view:

if concept.tag:
    concept.tag.delete()
concept.save()


I'm getting this error in line concept.save()

ValueError: save() prohibited to prevent data loss due to unsaved related object 'tag'.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa4a2b19-eaab-4e16-a255-09e910d0444d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Working with Django signals

I suggest you to change the user creation form to set the three attributes that you need (Is medical, is physuitherapist, is patient), so in the first form (when you create the user) you will have usernarme, password, Is_medical, is_physuitherapist, is_patient.

Take a look
Or extends admin class in save_model function.

2015-12-30 17:14 GMT-06:00 Bernardo Garcia <botibagl@gmail.com>:

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 so:


from __future__ import unicode_literals    from django.conf import settings  from django.contrib.auth.models import AbstractUser  from django.db import models    #from django.contrib.auth import get_user_model      from django.dispatch import receiver  from django.db.models.signals import post_save      # Extending Django's default User  # https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-django-s-default-user  # We inherit from AbstractUser class to add some fields/attibutes to our user model  # https://github.com/django/django/blob/master/django/contrib/auth/models.py#L297  # Differentes between AbstractUser and AbstractBaseUser  # http://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django  class User(AbstractUser):      is_medical = models.BooleanField(default=False)      is_physiotherapist = models.BooleanField(default=False)      is_patient = models.BooleanField(default=False)      slug = models.SlugField(max_length=100, blank=True)      photo = models.ImageField(upload_to='avatars', null = True, blank = True)          # We get the profiles user according with their type      def get_medical_profile(self):          medical_profile = None          if hasattr(self, 'medicalprofile'):              medical_profile=self.medicalprofile          return medical_profile        def get_patient_profile(self):          patient_profile = None          if hasattr(self, 'patientprofile'):              patient_profile = self.patientprofile          return patient_profile        def get_physiotherapist_profile(self):          physiotherapist_profile = None          if hasattr(self, 'physiotherapistprofile'):              physiotherapist_profile = self.physiotherapistprofile          return physiotherapist_profile        # We redefine the attributes (create db_table attribute) in class Meta to say to Django      # that users will save in the same table that the Django default user model      # https://github.com/django/django/blob/master/django/contrib/auth/models.py#L343      class Meta:            db_table = 'auth_user'    class MedicalProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      class PatientProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      class PhysiotherapistProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      """  So we're defined a signal for the User model, that is triggered every time a User instance is saved    The arguments used in the create_profile_for_new_user  are:     sender: the User model class     created: a boolean indicating if a new User has been created     instance: the User instance being saved  """  @receiver(post_save, sender=settings.AUTH_USER_MODEL)  #def create_profile_for_new_user(sender, instance, created, **kwargs):  def create_profile_for_new_user(sender, instance, created, **kwargs):      user = instance      # ----------------- Begin debug----------------------      import ipdb      #ipdb.set_trace()      #------------------------      if created:          #ipdb.set_trace()          if user.is_medical:              ipdb.set_trace()              profile=MedicalProfile(user=instance)              profile.save()    """  This signal checks if a new instance of the User model has been created,  and if true, it creates a Profile instance with using the new user instance.  """


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 (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:


In the code section in where the user instance that has been created is sent


enter image description here



  1. 2. 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): 


enter image description here



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:

  • sendercreatedinstance (this mean, the parameters of the function)
  • user, that is the same instance variable

enter image description here


The above,denote that the parameters are arriving. All is O.K for the moment.



  1. 4. Moving the debug

  2. Then, I perform the debug a few more below in the code (red square in the following figure) and I analyze the following:

enter image description here


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:


enter image description here



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

enter image description here

  • And the second phase when I enter the other fields or attributes in the creation of the that same user.

And, is in that same phase in which I tell to Django that my user will be or will have the medical profile, checking the is_medical checkbox attribute


enter image description here




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):

enter image description here


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. :(


All this, carry me to the following question:


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 stackoverflow 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 users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/142d89ce-4f3b-4c5f-af7b-cd8a6ff90fea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
"La utopía sirve para caminar" Fernando Birri


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG%2B5VyPHzh%2B2h%3DyFqM0hPO5gPkY98wEFLU-KTu3Ohjm1TraQqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: wsgi.py apache segmentation fault

Is code working right via manage runserver?

30 груд. 2015 13:59 "Baris Adrian" <padrian.baba@gmail.com> пише:
hi Eugenio,

Did you get a solution to this problem.

If so what? because i am stuck and i could appreciate the help.

Kindest REgards

On Thursday, October 8, 2015 at 5:45:40 PM UTC+3, Eugenio Trumpy wrote:
Hello everybody,

I'm very not so expert on django, I'm at the first experience.
I'm using django as backhand of geonode application.

Geonode works quite fine, however during a save map request from client side I got this apache2 segmentation fault:

[Thu Oct 08 15:30:38.037330 2015] [core:error] [pid 10101] [client X.X.X.X:50787] End of script output before headers: wsgi.py, referer: http://my_server_name/maps/new
[Thu Oct 08 15:30:38.292052 2015] [core:notice] [pid 2697] AH00052: child pid 10004 exit signal Segmentation fault (11)

I discovered this page on similar issue:

https://code.google.com/p/modwsgi/wiki/FrequentlyAskedQuestions

but I checked and I have not the mod_python installed.

Can somebody give me hints on how to solve this problem?

Best

Eugenio

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6042667-df1e-47c2-b8dd-6ce5046ae672%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADTRxJNC--8VcK0QonQm6Ms%3DsHTEEb8V3-vARhEKBvBxznBzPA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: how to restrict responses to a single server/domain ?

Thanks for recommendation. 

On Thursday, 31 December 2015 12:54:33 UTC+5:30, James Schneider wrote:


I suppose my answer did leave out the ability to check the IP at the Django level and make decisions based on that information. Given the multiple ways that an IP address may be made available to a Django application, I would recommend a package such as django-ipware (https://github.com/un33k/django-ipware) which handles a majority of these situations in a DRY manner, although I've never used it myself. The IP checking would be made part of your authorization checks that should be run for each request cycle. That implementation is specific to your environment, so I can't really comment further, although Django 1.9 made some great strides in making such permission checks easy to integrate. For <1.9, these checks would probably muddy up your code, but are still possible, especially if django-braces is used (which is the package that the authorization changes to 1.9 are modeled after).

I doubt I would ever do source IP checks in the application. That's what firewalls are for. Honestly, I'd much rather go with a true API key system, as it is much more flexible (especially if your API consumers live in multiple IP address segments that may/probably will change over time) and provides similar protection. If you find a trusted host is compromised, you simply invalidate the API key. Other processes using a different API key can continue to function from the same host. With strictly IP checking and no other authorization, you have to block the host entirely, which may or may not be acceptable. 

Full disclosure, I'm a network administrator by trade, so I'm generally biased towards network-level protections, and under the (usually correct) assumption that most application developers do a poor job of securing their applications, even when those applications cost more than a year's salary in licensing and are backed by Fortune 500 companies. Not that all applications are that way, but personal experience has left me a bit cynical and distrusting. Heck, we have big-money applications that still send service credentials across the network using straight HTTP and no encryption. App admins get mad when they create a new service account, and five minutes later you pull it off the network and read the password back to them while troubleshooting. Fun for me, though. :-D

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/513b2169-3f7a-4ef9-be63-5d3f1948d37c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: how to restrict responses to a single server/domain ?

You may simple use nginx and have multiple server blocks, one for each domain, all listening on the same port (80 or 443)

On Dec 30, 2015 10:11 AM, "Abraham Varricatt" <abraham.varricatt@googlemail.com> wrote:
I'm trying to build something similar to a microservice using Django and can't figure out how to setup the authentication part. 

When I say 'microservice' I mean, we have a bunch of systems like - example.com  , test.com  , another.com and another.test.com (a subdomain). I'm going to build a REST API on test.com which needs to be consumed by another.com. How can I enforce this restriction? And if later, I wanted to allow example.com to use the API (or even allow general public access), how can I do this?

My instincts tell me that I'm missing something obvious, but I can't seem to figure out what to search for.

Puzzled,
Abraham V.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/93f24cbb-3ab5-4903-ba78-89446b629e30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFWa6tJtETi_kLNPOMQoS6sC%3D%3DdEYwEv3wuuRLB%3Dd4CkFinBtA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: No such table error when using threads

Thank YOU!!!!!!!

On Thursday, November 5, 2009 at 10:58:55 AM UTC+2, BlueBird wrote:

Ok, I found a way around the problem, in case anyone ever has the same
problem. If you specify TEST_DATABASE_NAME in your settings, it will
force sqllite to use a file database instead of in-memory database,
and then the problem goes.

cheers,

Philippe

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/581ccc59-575a-4fab-b321-01a405cca541%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Delete confirmation page in Django admin not displaying child records

Using Django 1.6.11

I have just noticed for a few cases (at least) that, while delete confirmation page in Django admin shows as usual in my app, it does not also show the related child records that will be deleted when a parent record is selected (the confirmation page only shows the parent record).  I ran a separate test using this code - http://thirld.com/blog/2012/09/17/django-enumerate-related-objects-cascade-deleted/ - which shows that there, in fact, related records.

(I do not - yet - have tests that would have showed me when this functionality stopped working; I had assumed that, because it was part of Django, I would not need tests for this.)

Any ideas why this happening and how to "revert" to the expected behaviour?

Thanks
Derek

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3NhA9TeDiX9Dcra88B5--MD_Z37Amf-LF0YbVc6%3DgEknQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Wednesday, December 30, 2015

Re: how to restrict responses to a single server/domain ?



On Wed, Dec 30, 2015 at 5:14 AM, Billu <billu.mandal@gmail.com> wrote:
Wouldn't it be much easier to just check the IP (or mac) addresses of the request and if it matches the allowable ip(s), then allow otherwise deny?

That's the super short and incomplete version of my response. My answer went more into depth about the various methods to do so.

MAC addresses are only relevant if a) all of the machines exist within the same Ethernet segment/VLAN and b) only if you have a host or hardware firewall capable of making policy decisions based on MAC address. Django will never see the MAC address, as it is stripped away by the OS prior to the request being processed. TL;DR; You'll probably never refer to MAC addresses and Django security in the same sentence.

"Just check the IP" is missing the vital answer of "how", which is a key portion in the first line of the OP's question.

I suppose my answer did leave out the ability to check the IP at the Django level and make decisions based on that information. Given the multiple ways that an IP address may be made available to a Django application, I would recommend a package such as django-ipware (https://github.com/un33k/django-ipware) which handles a majority of these situations in a DRY manner, although I've never used it myself. The IP checking would be made part of your authorization checks that should be run for each request cycle. That implementation is specific to your environment, so I can't really comment further, although Django 1.9 made some great strides in making such permission checks easy to integrate. For <1.9, these checks would probably muddy up your code, but are still possible, especially if django-braces is used (which is the package that the authorization changes to 1.9 are modeled after).

I doubt I would ever do source IP checks in the application. That's what firewalls are for. Honestly, I'd much rather go with a true API key system, as it is much more flexible (especially if your API consumers live in multiple IP address segments that may/probably will change over time) and provides similar protection. If you find a trusted host is compromised, you simply invalidate the API key. Other processes using a different API key can continue to function from the same host. With strictly IP checking and no other authorization, you have to block the host entirely, which may or may not be acceptable. 

Full disclosure, I'm a network administrator by trade, so I'm generally biased towards network-level protections, and under the (usually correct) assumption that most application developers do a poor job of securing their applications, even when those applications cost more than a year's salary in licensing and are backed by Fortune 500 companies. Not that all applications are that way, but personal experience has left me a bit cynical and distrusting. Heck, we have big-money applications that still send service credentials across the network using straight HTTP and no encryption. App admins get mad when they create a new service account, and five minutes later you pull it off the network and read the password back to them while troubleshooting. Fun for me, though. :-D

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUo2abWpuQp_q_eFJXn1bFtS2QSFr0-OnLZOTfm%2BOa_%2Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Working with Django signals

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 so:


from __future__ import unicode_literals    from django.conf import settings  from django.contrib.auth.models import AbstractUser  from django.db import models    #from django.contrib.auth import get_user_model      from django.dispatch import receiver  from django.db.models.signals import post_save      # Extending Django's default User  # https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-django-s-default-user  # We inherit from AbstractUser class to add some fields/attibutes to our user model  # https://github.com/django/django/blob/master/django/contrib/auth/models.py#L297  # Differentes between AbstractUser and AbstractBaseUser  # http://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django  class User(AbstractUser):      is_medical = models.BooleanField(default=False)      is_physiotherapist = models.BooleanField(default=False)      is_patient = models.BooleanField(default=False)      slug = models.SlugField(max_length=100, blank=True)      photo = models.ImageField(upload_to='avatars', null = True, blank = True)          # We get the profiles user according with their type      def get_medical_profile(self):          medical_profile = None          if hasattr(self, 'medicalprofile'):              medical_profile=self.medicalprofile          return medical_profile        def get_patient_profile(self):          patient_profile = None          if hasattr(self, 'patientprofile'):              patient_profile = self.patientprofile          return patient_profile        def get_physiotherapist_profile(self):          physiotherapist_profile = None          if hasattr(self, 'physiotherapistprofile'):              physiotherapist_profile = self.physiotherapistprofile          return physiotherapist_profile        # We redefine the attributes (create db_table attribute) in class Meta to say to Django      # that users will save in the same table that the Django default user model      # https://github.com/django/django/blob/master/django/contrib/auth/models.py#L343      class Meta:            db_table = 'auth_user'    class MedicalProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      class PatientProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      class PhysiotherapistProfile(models.Model):      user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)      #active = models.BooleanField(default=True)      name = models.CharField(max_length=64)      """  So we're defined a signal for the User model, that is triggered every time a User instance is saved    The arguments used in the create_profile_for_new_user  are:     sender: the User model class     created: a boolean indicating if a new User has been created     instance: the User instance being saved  """  @receiver(post_save, sender=settings.AUTH_USER_MODEL)  #def create_profile_for_new_user(sender, instance, created, **kwargs):  def create_profile_for_new_user(sender, instance, created, **kwargs):      user = instance      # ----------------- Begin debug----------------------      import ipdb      #ipdb.set_trace()      #------------------------      if created:          #ipdb.set_trace()          if user.is_medical:              ipdb.set_trace()              profile=MedicalProfile(user=instance)              profile.save()    """  This signal checks if a new instance of the User model has been created,  and if true, it creates a Profile instance with using the new user instance.  """


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 (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:


In the code section in where the user instance that has been created is sent


enter image description here



  1. 2. 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): 


enter image description here



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:

  • sendercreatedinstance (this mean, the parameters of the function)
  • user, that is the same instance variable

enter image description here


The above,denote that the parameters are arriving. All is O.K for the moment.



  1. 4. Moving the debug

  2. Then, I perform the debug a few more below in the code (red square in the following figure) and I analyze the following:

enter image description here


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:


enter image description here



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

enter image description here

  • And the second phase when I enter the other fields or attributes in the creation of the that same user.

And, is in that same phase in which I tell to Django that my user will be or will have the medical profile, checking the is_medical checkbox attribute


enter image description here




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):

enter image description here


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. :(


All this, carry me to the following question:


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 stackoverflow 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 users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/142d89ce-4f3b-4c5f-af7b-cd8a6ff90fea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.