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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:01:32+00:00 2026-05-27T13:01:32+00:00

I have a fairly simple Django application (v1.3 on Red Hat) for which I’m

  • 0

I have a fairly simple Django application (v1.3 on Red Hat) for which I’m using the admin application to create and modify database records. One of the fields in my underlying model is a date field. Each time the corresponding field is displayed in the admin’s new or edit form I’d like the initial value of this field to be today’s date (and time). The user may choose to modify it thereafter, if she desires.

I know that I can set the default field value within my model definition (i.e. in models.py). Which works fine when a database record is first created. But for subsequent invocations of the change form the callable that I’ve assigned to the default parameter (datetime.datetime.now) obviously doesn’t get invoked.

I’ve looked at – and tried – pretty well all of the many proposed solutions described elsewhere in stackoverflow, without success. Most of these appear to revolve around inserting initialisation code into the ModelForm subclass, e.g. either something like this…

class ConstantDefAdminForm(ModelForm) :
    a_date_field = DateField(initial="datetime.datetime.now")  # or now()
    class Meta :
        model = ConstantDef
        widgets = {
            ...
        }

or something like this…

class ConstantDefAdminForm(ModelForm) :
    class Meta :
        model = ConstantDef
        widgets = {
            ...
    }
    def __init__(self, ...) :
        # some initialisation of a_date_field
        super(ConstantDefAdminForm, self).__init__(...)

But neither of these approaches work. The initial field value is always set to the value that is stored in the database. My reading of the Django documentation is that the various ways of imposing initial field values in forms only work for unbound forms, not bound forms. Right?

But this capability (to selectively override currently stored values) would seem to be such a popular requirement that I’m convinced that there must be a way to do it.

Has anyone out there succeeded in doing this?

Thanks in advance,

Phil

  • 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-27T13:01:32+00:00Added an answer on May 27, 2026 at 1:01 pm

    Here’s an approach that might work. In your model admin class, change the value of obj.a_date_field before the form is bound. The ‘default’ value for the date field should be the new value.

    class MyModelAdmin(ModelAdmin):
        ...
        def get_object(self, request, object_id):
            obj = super(MyModelAdmin, self).get_object(request, object_id)
            if obj is not None:
                obj.a_date_field = datetime.now()
            return obj
    

    Note that get_object is not documented, so this is a bit hacky.

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

Sidebar

Related Questions

I have a fairly simple data audit web application written with ASP MVC which
I have a django application using mod_python, fairly typical configuration except that media files
I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple database that I inherited. For the purposes of this
I have a fairly simple Windows Forms application that I would like to add
I have a fairly simple requirement for a query API which I need to
I have a django site running with a mysql database backend. I accept fairly
I have a fairly simple desktop application that uses a .NET setup project (.msi
I have a fairly simple Rails application that allows users to manage their clients

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.