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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:59:44+00:00 2026-05-28T17:59:44+00:00

I am overriding the save_model method of my ModelAdmin so I can set some

  • 0

I am overriding the save_model method of my ModelAdmin so I can set some additional fields on the model. However, I keep getting a DoesNotExist exception. I stepped through the code with a debugger and I’m a bit confused.

The model has this field:

created_by = models.ForeignKey(User, db_column='created_by', related_name='%(class)s_creator')

In the save_model method when I look at the attributes of the obj parameter, I see this:

Name:
created_by

Value:
str: Traceback (most recent call last):
  File "/home/vicki/.eclipse/org.eclipse.platform_3.7.0_155965261/plugins/org.python.pydev.debug_2.3.0.2011121518/pysrc/pydevd_resolver.py", line 182, in _getPyDictionary
    attr = getattr(var, n)
  File "/usr/lib/pymodules/python2.7/django/db/models/fields/related.py", line 301, in __get__
    raise self.field.rel.to.DoesNotExist
DoesNotExist

Name:
created_by_id

Value:
NoneType: None

Where did ‘created_by_id’ come from, and why does ‘created_by’ not exist?

  • 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-28T17:59:45+00:00Added an answer on May 28, 2026 at 5:59 pm

    Is there a particular reason why you have used db_column='created_by'? Are you dealing with a legacy database? Normally, you would let Django handle the name of the db column. By default, Django will append _id to the field name to create the db column name. This is explained in the Django Foreign Key docs.

    On your model instance you can access:

    `obj.created_by_id` # the primary key of the related User instance
    `obj.created_by` # The `User` instance
    

    You can’t access obj.created_by in your method because you haven’t assigned a user to it yet. The created_by_id value is None, and trying to fetch a User with primary key None naturally raises a DoesNotExist exception.

    Your save_model method should look something like this:

    class MyModelAdmin(admin.ModelAdmin):
        def save_model(self, request, obj, form, change):
            obj.created_by = request.user
            obj.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code overriding the save method of a model:
While overriding the specific model's save() method, Is it possible determine whether it's a
I need to detect when some of the fields of certain model have changed
I'm having trouble overriding a ModelForm save method. This is the error I'm receiving:
when overriding the loadView method in UIViewController, should one call [super loadView] in the
I am overriding a Grid, adding some customer features. One of the features is
I understand overriding a method/function redefines its implementation in the derived class from its
I am overriding the clean() method on a Django form. I want to have
I want to execute some code in a Django model when it is first
Is there any way to access sessions variables while overriding any models save method

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.