Setting the time on a datetimefield

1,409 views
Skip to first unread message

Tim Sawyer

unread,
Jul 14, 2008, 12:58:08 PM7/14/08
to django...@googlegroups.com
Hi Folks,

I have an object with a model.DateTimeField on it. For various reasons, I
need to set the time portion of this field to 23:59 when I populate it from a
form.

datetime = models.DateTimeField()

How can I do this? I've tried

self.datetime = pForm.cleaned_data['datetime']
self.datetime.replace(hour=23, minute=59)

but hour isn't accepted as a parameter to replace.

I'm also confused as to how the DateTimeField maps on to a python datetime
object, can anyone educate me?

Thanks,

Tim.

Rajesh Dhawan

unread,
Jul 14, 2008, 3:22:36 PM7/14/08
to Django users
Hi Tim,

> I have an object with a model.DateTimeField on it. For various reasons, I
> need to set the time portion of this field to 23:59 when I populate it from a
> form.
>
> datetime = models.DateTimeField()
>
> How can I do this? I've tried
>
> self.datetime = pForm.cleaned_data['datetime']
> self.datetime.replace(hour=23, minute=59)

The replace method here returns a new instance of a datetime object --
it's not an in-place replace. This should work:

self.datetime = self.datetime.replace(hour=23, minute=59)

That works both when datetime is a forms.DateTimeField or
models.DateTimeField.

>
> but hour isn't accepted as a parameter to replace.

It is. Are you getting a Python error when you do that?

>
> I'm also confused as to how the DateTimeField maps on to a python datetime
> object, can anyone educate me?

You can directly assign a Python datetime object to a
models.DateTimeField.

-Rajesh D

Tim Sawyer

unread,
Jul 14, 2008, 5:40:40 PM7/14/08
to django...@googlegroups.com
On Monday 14 Jul 2008, Rajesh Dhawan wrote:
> The replace method here returns a new instance of a datetime object --
> it's not an in-place replace. This should work:
>
> self.datetime = self.datetime.replace(hour=23, minute=59)

Hmm, that seemed to make sense to me too, though with my Java background I
wasn't sure. I get:

Exception Value: 'hour' is an invalid keyword argument for this function

on

self.datetime = self.datetime.replace(hour=23, minute=59)

self.datetime is defined as:

datetime = models.DateTimeField()

Cheers,

Tim.

Rajesh Dhawan

unread,
Jul 14, 2008, 6:37:26 PM7/14/08
to Django users
Strange...I just ran a few tests on a model field like that and it all
worked perfectly.

Can you dpaste your relevant code?

Also, if you print type(self.datetime), do you get the type as
datetime.datetime or something else?

Tim Sawyer

unread,
Jul 15, 2008, 5:57:50 AM7/15/08
to django...@googlegroups.com

Sussed it.

In my code, I did this:

self.datetime = pForm.cleaned_data['datetime']

the datetime parameter passed from the form only contained the date, with no
time, so I assume the value in datetime then was only a date.

I've sorted my problem using the following code:

self.datetime = datetime(self.datetime.year, self.datetime.month,
self.datetime.day, 23, 59)

Thanks for your help,

Tim.

Arien

unread,
Jul 15, 2008, 7:03:46 AM7/15/08
to django...@googlegroups.com
On Tue, Jul 15, 2008 at 4:57 AM, Tim Sawyer <list....@calidris.co.uk> wrote:
> In my code, I did this:
>
> self.datetime = pForm.cleaned_data['datetime']
>
> the datetime parameter passed from the form only contained the date, with no
> time, so I assume the value in datetime then was only a date.

This shouldn't happen: a DateTimeField normalizes to a
datetime.datetime object. Are you sure you're not using a DateField
in your form?


Arien

Tim Sawyer

unread,
Jul 15, 2008, 8:25:22 AM7/15/08
to django...@googlegroups.com

Well spotted.

In my form I have:

datetime =
forms.DateField(widget=forms.TextInput({'class' : 'date-pick'}),label="Available
date")

yet it's a DateTimeField in the model.

This means that the reason that the value fetched from the form is only a date
is because of the type in the form. Assigning this date only value from the
form to the model attribute means I can't use replace(hour=23) on it any
more. I don't really understand why, but I'm sure there's a logical
explanation!

Tim.

Arien

unread,
Jul 15, 2008, 8:55:38 AM7/15/08
to django...@googlegroups.com
On Tue, Jul 15, 2008 at 7:25 AM, Tim Sawyer <list....@calidris.co.uk> wrote:
> In my form I have:
>
> datetime =
> forms.DateField(widget=forms.TextInput({'class' : 'date-pick'}),label="Available
> date")
>
> yet it's a DateTimeField in the model.

Why not use a DateTimeField in your form and change the widget
instead? For example:

datetime = forms.DateTimeField(
widget=forms.DateTimeInput(format='%Y-%m-%d', attrs={'class' :
'date-pick'}),
label="Available date"
)

(Note the attrs={...} for the class.)

> This means that the reason that the value fetched from the form is only a date
> is because of the type in the form. Assigning this date only value from the
> form to the model attribute means I can't use replace(hour=23) on it any
> more. I don't really understand why, but I'm sure there's a logical
> explanation!

The logical explanation is that adjusting the hour of a date doesn't
make sense, because a date doesn't have any hour. ;-)


Arien

Reply all
Reply to author
Forward
0 new messages