Opened 17 years ago

Closed 17 years ago

Last modified 16 years ago

#6626 closed (worksforme)

newforms-admin DateField populated value wrong

Reported by: Dan <danielnaab@…> Owned by: nobody
Component: contrib.admin Version: newforms-admin
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With the admin in newforms-admin, DateFields are getting populated with the complete datetime string including hours, minutes, seconds. This causes validation to fail on subsequent saves because that datetime format is not part of DateFields default input_formats.

To reproduce:
\

class MyModel(models.Model):
    date = models.DateField()

Create an instance of MyModel with a valid date in the admin.  Save.  Reload and save again, and date validation will fail.  Looking at the contents of the date's field in the form, you'll see that it's postfixed with 00:00:00 (you may have to right-arrow over to catch it... in Safari I do).

I've come up with a workaround that may be of use to some people.  In MyModel's ModelAdmin, place:
{{{
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == u'date_in':
            MY_DATE_INPUT_FORMATS = (
                # here's our custom input to handle the time that is getting added at the end of the widget:
                '%Y-%m-%d',                         # '2006-10-25'
                '%Y-%m-%d %H:%M:%S',                # '2006-10-25 00:00:00'
            )
            field = super(CanvasserResultsOptions, self).formfield_for_dbfield(db_field, **kwargs)
            field.input_formats = MY_DATE_INPUT_FORMATS
            return field
        return super(CanvasserResultsOptions, self).formfield_for_dbfield(db_field, **kwargs)
}}}

This simply overrides the input_formats to the two formats the admin interface might encounter.

Change History (5)

comment:1 by Dan <danielnaab@…>, 17 years ago

My apologies for screwing up the formatting. Here it is again:

With the admin in newforms-admin, DateFields are getting populated with the complete datetime string including hours, minutes, seconds. This causes validation to fail on subsequent saves because that datetime format is not part of DateFields default input_formats.

To reproduce:

class MyModel(models.Model):
    date = models.DateField()

Create an instance of MyModel with a valid date in the admin. Save. Reload and save again, and date validation will fail. Looking at the contents of the date's field in the form, you'll see that it's postfixed with 00:00:00 (you may have to right-arrow over to catch it... in Safari I do).

I've come up with a workaround that may be of use to some people. In MyModel's ModelAdmin, place:

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == u'date_in':
            MY_DATE_INPUT_FORMATS = (
                # here's our custom input to handle the time that is getting added at the end of the widget:
                '%Y-%m-%d',                         # '2006-10-25'
                '%Y-%m-%d %H:%M:%S',                # '2006-10-25 00:00:00'
            )
            field = super(CanvasserResultsOptions, self).formfield_for_dbfield(db_field, **kwargs)
            field.input_formats = MY_DATE_INPUT_FORMATS
            return field
        return super(CanvasserResultsOptions, self).formfield_for_dbfield(db_field, **kwargs)

This simply overrides the input_formats to the two formats the admin interface might encounter.

comment:2 by Brian Rosner, 17 years ago

Resolution: worksforme
Status: newclosed

I just tested this and didn't see any problems with r7125. Make sure you are at HEAD. If you are please provide some more concrete code example to help us better understand what is going on here. Thanks.

comment:3 by Brian Rosner, 17 years ago

One thing you will want to make sure is that you are using datetime.date.today as the callable to default as opposed to datetime.datetime.now.

comment:4 by Dan <danielnaab@…>, 17 years ago

brosner,

I am indeed working off the head revision, but I found this bug was specific to my database - after creating a new database, I found that my date column's default value was not set. Don't know how that relates to this, but it solved the problem.

Sorry for posting this as a "bug"

comment:5 by Tom Clancy, 16 years ago

I created this problem for myself by initially setting a field as a DateTimeField, creating my tables in the database and then changing the field in the model to DateField. MySQL had the column as DATETIME and was passing back a full timestamp to Django. I updated to a DATE field and everything's fine.

Note: See TracTickets for help on using tickets.
Back to Top