[Answer]-Cannot create Django user due to UserProfile creation throwing IntegrityError

1👍

Answered my own question. When a User is created & saved, the UserProfile is created/saved as well – this we know. What I did not know is that Dates, Booleans and integers cannot be null as stated here. So, the solution is to change this line of the UserProfile class:

age = models.DateField()

to

age = models.DateField(null=True)

This allows the UserProfile to be saved to the DB without being populated, and for me to retrieve it with get_profile() and populate it later.

👤BigJim

0👍

Did you check if age is not empty?
Why not to create user via userprofile? What i mean is:

profile.first_name = 'abc'
profile.last_name = 'abc'
profile.age = Date(2000, 1, 1)
profile.save()
👤szaman

0👍

I think you have to add AUTH_PROFILE_MODULE to you settings.py so as to automatically create a user profile after a new user is saved. Did you check this documentation?

Leave a comment