Code

Opened 6 years ago

Closed 6 years ago

Last modified 5 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: UI/UX:

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.

Attachments (0)

Change History (5)

comment:1 Changed 6 years ago by Dan <danielnaab@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 6 years ago by brosner

  • Resolution set to worksforme
  • Status changed from new to closed

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 Changed 6 years ago by brosner

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 Changed 6 years ago by Dan <danielnaab@…>

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 Changed 5 years ago by tclancy

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.