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 562595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:34:16+00:00 2026-05-13T12:34:16+00:00

I have a problem when I update an object. This is the model: class

  • 0

I have a problem when I update an object. This is the model:

class HourRecord(models.Model):
    day_of_work = models.PositiveIntegerField(max_length=1)
    date_of_work = models.DateField(verbose_name='creation date')
    who_worked = models.ForeignKey(User)
    project = models.ForeignKey(Project, related_name='hour_record_set', null=True)

    created_by = models.ForeignKey(User, editable=False, related_name='hour_record_creator')
    created = models.DateTimeField(auto_now_add=True, editable=False, verbose_name='creation date')
    modified_by = models.ForeignKey(User, editable=False, related_name='hour_record_modifier')
    modified = models.DateTimeField(auto_now=True, editable=False)


    def save(self, request):
        if not self.id:
            self.who_worked = request.user
            self.created_by = request.user
        self.modified_by = request.user
        super(HourRecord, self).save()

The created_by field should only be set if its a new object. Therefore the “if not self.id:”. This worked fine till now.

Till now I updated the object like this:

if hrform.is_valid():
            hour_record = hrform.save(commit=False)
            hour_record.save(request)

But when I update the model like this I get an error:

project = Project.objects.get(id=project_pk)
hr_object = HourRecord(id=hour_record_pk, day_of_work=weekday, date_of_work=date, who_worked=request.user, project=project)
hr_object.save(request)

The error message is:

Column 'created_by_id' cannot be null

Thats strange to me since the created_by column has been already set. I checked it in the database. After this error message when I check again the object is updated and created_by_id is set to null. Actually this is strange. I get an error message + the row is updated.

The second approach is called from within a json data handling view. But I don’t think that it has anything to do with it. I think I can circumvent this problem when I reset the created_by_id, but that is not what I want to do, since it corrupts the logic of this created_by field.

  • 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-13T12:34:16+00:00Added an answer on May 13, 2026 at 12:34 pm

    Well, the issue appears to be that you are explicitly setting the id field when you instantiate the HourRecord object:

    hr_object = HourRecord(id=hour_record_pk,...
    

    So when you get to the save() method, it checks if it has an ID… and it does, so it doesn’t set who_worked and created_by.

    I wonder why you need to set the ID. Normally, you should let the database set it automatically via the autoincrement.

    (Not related to your issue, but your save method should accept the force_update and force_insert parameters, and pass them to the super method. The easiest way to do this is to get into the habit of always using *args, **kwargs when overriding a method.)

    Edit: To answer your question of why this doesn’t work like an update statement, it’s because you’re explicitly replacing the old object with a new one. In effect, you’re deleting the old db entry and inserting a completely new set of data. There’d be no way for Python to know whether you intended to keep a field’s old value, or set it to NULL, for example.

    The way to do this, as you have noted, is to get the existing object and updating its attributes explicitly.

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

Sidebar

Ask A Question

Stats

  • Questions 293k
  • Answers 293k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This issue was fix by updating the mailer plugin in… May 13, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer You created new copy which you don't release. This returns… May 13, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer Hibernate seems to do following: When hibernate needs a primary… May 13, 2026 at 6:23 pm

Related Questions

I encountered a problem when I tried to implement the ServiceKnownType example from MSDN.
I am wondering if there is a way to make ASP.NET controls play nicely
I have a problem when updating the source of an ASP ListBox in an
I have a question on how to update an existing row in my database
I have a situation where I have an interface that defines how a certain

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.