I am having problem saving additional information about each user automatically when a new user signs-up. I have created a profile for User model extension to save additional data about my users. However, when the signal handler gets called at post_save, the data stored in the request is not passed to the signal_handler.
The following is the profile that I have created.
class StudentProfile(models.Model):
user = models.ForeignKey(User, unique=True)
# Other information about the Student
city = models.CharField(max_length=25)
country = models.CharField(max_length=25)
student_age = models.IntegerField(max_length=3)
In my signup view I call form.save() function in which the User object gets created and a new row is added in the data base.
def save(self, new_data):
u = User.objects.create_user( new_data['username'],
new_data['email'],
new_data['password'])
return u
After that, the post_save signal is called and the signal handler tries to create a new profile:
def create_student_profile(sender, instance, created, **kwargs):
if created:
StudentProfile.objects.create(user=instance)
post_save.connect(create_student_profile, sender=User)
At this point, I get the following error in the broswer:
IntegrityError at /signup/
(1048, “Column ‘student_age’ cannot be null”)
I looked at the call stack and found that the SQL query that is being executed has None value for Student_age and nothing for country and city.
sql u’INSERT INTO student_studentprofile (user_id, city, country, student_age) VALUES (17, , , None)’
How can I pass the request parameters such as student_age, country and city to this signal handler? Do I have to manually save this information?
I have thoroughly searched for the answer of this question on stackOverflow and Google and I have come rather empty handed.
Thanks for your help.
Yep, unfortunately you’ll need to make those fields nullable. Then set them in your view after the user has been saved.