Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:12:53+00:00 2026-06-10T11:12:53+00:00

I try to save user’s birth date, but get null value in column dob

  • 0

I try to save user’s birth date, but get “null value in column “dob” violates not-null constraint” error.

models.py:

class Profile(models.Model):
  user = models.OneToOneField(User, unique=True)

  nickname = models.CharField(max_length=32)
  dob = models.DateField(null=False)
  sex = models.BooleanField(null=False)

Here i try to generate random users:

def create_random_users(userCount=1000):
  random.seed()

  for i in range(0, userCount):
    sex = random.randint(0, 1)
    name = random.choice(names[sex])

    email = "{0}{1}@mail.com".format(name, i)
    user = soc_models.User.objects.create_user(email, email, password='password')
    user.save()

    userProfile = soc_models.Profile.objects.create()
    userProfile.user = user
    _year = random.randrange(1962, 1995)
    _month = random.randrange(1, 12)
    _day = random.randrange(1, calendar.monthrange(_year, _month)[1])
    userProfile.dob = datetime.datetime(_year, _month, _day)

    userProfile.sex = random.randrange(0, 1)
    userProfile.city = random.randrange(4000000)
    userProfile.country = random.randrange(230)
    userProfile.save()

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:12:54+00:00Added an answer on June 10, 2026 at 11:12 am

    The create method is documented as “a convenience method for creating an object and saving it all in one step”. So when the following statement in your sample data creation script runs:

    userProfile = soc_models.Profile.objects.create()
    

    It attempts to save an empty Profile object to the database. Since you haven’t set the dob attribute at this point, you trigger the NOT NULL constraint.

    Two ways to avoid this are:

    1. create the object via the constructor so that it isn’t immediately saved to the database.
    2. provide values for all the fields via keyword arguments to create.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i try to save user settings in c#. I can read the value and
When I try to save images to a directory, it's not saving to my
When I try and save something to my list in SharePoint I get the
When I try to save a certain of my ActiveRecord instances, I get this
when I try to Save an updated managedobject the changes don't get persisted to
ZipFileToCreate = c:\user\desktop\webservice\file.zip; So, when i try to save this file it creates the
I have an INSERT wrapped in a try/catch, but a missing table is not
i try to save user session in a hashmap on every cluster. and when
When I try to modify and then save a model using DataMapper I get
I try to save user settings. To save some data I used this code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.