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

  • Home
  • SEARCH
  • 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 6609927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:46:34+00:00 2026-05-25T19:46:34+00:00

Following is the models.py: class UserProfile(models.Model): user = models.OneToOneField(User) belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)

  • 0

Following is the models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)

class UserHistory(models.Model):
    date_time = models.DateTimeField()
    user = models.ForeignKey(UserProfile, null=True, blank=True)
    points_earned = models.DecimalField(max_digits=5, decimal_places=3)

as is clear, userhistory is a foreign key to UserProfile. For the test purpose i wanted to update the points of the user whose name starts with a

I wrote the following code in the python shell:

from myapp.models import *
uobj = UserProfile.objects.all()
for i in uobj:
    if i.user.username[0] == 'a':
        b = UserHistory.objects.create(user=i)
        b.points_earned = random.random(10, 100)
        b.date_time = datetime.datetime.now()
        b.save()

i have also tried b = UserHistory.objects.get_or_create(user=i) with same error
and i get the following error:

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (160, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (13, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (63, 0))

ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/IPython/ultraTB.py", line 667, in text
    locals,formatvalue=var_repr))
  File "/usr/lib/python2.6/inspect.py", line 875, in formatargvalues
    specs.append(strseq(args[i], convert, join))
  File "/usr/lib/python2.6/inspect.py", line 830, in strseq
    return convert(object)
  File "/usr/lib/python2.6/inspect.py", line 872, in convert
    return formatarg(name) + formatvalue(locals[name])
KeyError: 'connection'

IPython's exception reporting continues...

---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)


IntegrityError: (1048, "Column 'date_time' cannot be null")
  • 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-05-25T19:46:34+00:00Added an answer on May 25, 2026 at 7:46 pm

    When you use the create method of the default model manager in Django, it will also attempt to create that instance into the database and save it. You can do this in two ways, but I’ll show you what you can do with your approach, which may help in some understanding.

    First, you will want to create a UserHistory object, but don’t actually save it. This is done by simply instantiating that model class with any default values you’d like:

    b = UserHistory(user=i)
    

    After that, you can set the other attributes.

    b.points_earned = random.randint(10, 100)
    b.date_time = datetime.datetime.now()
    b.save()
    

    And this will work. Because you’re now saving b after you’ve set the date_time.

    There are ways to improve this of course, you can simply create everything in one call since you’re doing it in one logical step, like so:

    b = UserHistory.objects.create(
      user=i,
      points_earned=random.randint(10, 100),
      date_time=datetime.datetime.now(),
    )
    

    You can further improve this by reading the DateField docs for Django, which will give you some tips on how to set the default date to the current time.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have the the following Django model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) full_name
I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True,
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
I have the following models: class Bill(models.Model): date = models.DateTimeField(_(Date of bill),null=True,blank=True) class Item(models.Model):
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have the following two models: class Provider(models.Model): provider = models.CharField(max_length=100, unique=True) class UserProfile(models.Model):
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user

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.