DateField returns 'str' object has no attribute 'strftime'

2,054 views
Skip to first unread message

Henrik Lied

unread,
Apr 20, 2007, 5:09:57 AM4/20/07
to Django users
Hi there,

I'm creating a user registration system for a project.
The system uses the SelectDateWidget for user input of birthdate.

When I try to register a new user, the system spits back this error:
'str' object has no attribute 'strftime'

This is the relevant code:
bd_month = frm.data['birthdate_month']
bd_day = frm.data['birthdate_day']
if len(bd_month) == 1:
bd_month = '0'+bd_month
if len(bd_day) == 1:
bd_day = '0'+bd_day

temp_date = frm.data['birthdate_year']+'-'+bd_month+'-'+bd_day

The error occurs in django/db/models/fields/__init__.py in
get_db_prep_save

Anyone have a clue?

Malcolm Tredinnick

unread,
Apr 20, 2007, 5:44:08 AM4/20/07
to django...@googlegroups.com

A few more clues might be helpful. The error message is telling you that
you're passing a string where a datetime object is expected, but you
don't explain how you are passing data to a model field anywhere. Note
that get_db_prep_save() is used to convert Python objects back to
strings for putting into the database, so if your field data has not
been already converted to a Python object by that point, a mistake has
been made somewhere.

What line in the above code fragment throughs the exception? If none of
those lines do it, how are you passing your data to the code that does
throw the exception? Can you write a short example that shows the
problem?

Regards,
Malcolm


Henrik Lied

unread,
Apr 20, 2007, 5:56:35 AM4/20/07
to Django users
Hi Malcolm,

I just figured it out - with some help in the IRC-channel.

I passed the form variables through datetime.date. That did the trick.

Thanks for your reply!

deez...@gmail.com

unread,
Apr 30, 2007, 12:39:17 PM4/30/07
to Henrik Lied, django...@groups.google.com
Would you mind posting your code snippet here. I have the same issue.
I thought the widget dealt with the creation of the datetime.date obj?

best regards,

David

deez...@gmail.com

unread,
Apr 30, 2007, 1:33:13 PM4/30/07
to Django users
Same problem:
----
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/django/core/handlers/base.py" in get_response
77. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/dahl/Code/telecom/confmngr/views.py" in detail
205. conf.save()
File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/django/db/models/base.py" in save
217. db_values = [f.get_db_prep_save(f.pre_save(self, False)) for f
in non_pks]
File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/
site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
494. value = value.strftime('%Y-%m-%d')

AttributeError at /telecom/conference/detail/9/
'tuple' object has no attribute 'strftime'
----
This happens on update of newform. What am I doing wrong?

view code:
-----------------
conf = Conference.objects.get(id=conf_id)
if request.POST:
#validate input:
if conf.caller == os.environ['REMOTE_USER']:
p = request.POST.copy()

form = ConferenceForm(p)
print "before is valid"
if form.is_valid():
print dir(form.clean_data['conference_date'])
#try:

conf.caller=form.clean_data['caller'],
conf.extension=form.clean_data['extension'],
conf.day_of_week=form.clean_data['day_of_week'],

conf.conference_date=form.clean_data['conference_date'],


conf.conference_time=form.clean_data['conference_time'],
conf.time_zone=form.clean_data['time_zone'],
conf.group_leader=form.clean_data['group_leader'],

conf.leader_extension=form.clean_data['leader_extension'],

conf.number_of_lines=form.clean_data['number_of_lines'],
conf.domestic_lines=form.clean_data['domestic_lines'],

conf.international_lines=form.clean_data['international_lines'],

conf.duration_of_call=form.clean_data['duration_of_call'],
conf.recurring_call=form.clean_data['recurring_call'],
conf.end_date=form.clean_data['end_date'],
conf.cost_code=form.clean_data['cost_code'],

conf.unattended_tone_in=form.clean_data['unattended_tone_in']
conf.save()

msg = "Conference #%s Updated" % conf_id

-----
conferenceForm:
-----

class ConferenceForm(forms.Form):
caller = forms.CharField(max_length=256,label="Caller/Requester",
initial=os.environ['REMOTE_USER'])
extension = forms.CharField(max_length=11,label="Extension")
day_of_week = forms.ChoiceField(label="Day of
Week",choices=dow_choices)
conference_date = forms.DateField(label="Conference Date")
conference_time = forms.TimeField(label="Conference Time")
time_zone = forms.CharField(max_length=4,label="Time Zone")
group_leader = forms.CharField(max_length=32,label="Group Leader")
leader_extension = forms.CharField(max_length=11,label="Leader
Extension")
number_of_lines = forms.CharField(max_length=11,label="Number of
Lines")
domestic_lines = forms.CharField(max_length=11,label="Domestic
Lines")
international_lines =
forms.CharField(max_length=11,label="International Lines")
duration_of_call = forms.TimeField(label="Call Duration")
recurring_call = forms.CharField(label="Recurring
Call",required=False,
widget=CheckboxInput())
end_date = forms.CharField(label="End Date",required=False)
cost_code = forms.CharField(max_length=32,label="Cost Code")
unattended_tone_in = forms.CharField(label="Unattended Tone-in",
widget=CheckboxInput(),
initial=True)

On Apr 30, 11:39 am, "deezth...@gmail.com" <deezth...@gmail.com>
wrote:

Phil Davis

unread,
Apr 30, 2007, 4:25:49 PM4/30/07
to django...@googlegroups.com
On 30/04/07, deez...@gmail.com <deez...@gmail.com> wrote:
>
[...]
> Same problem:

> AttributeError at /telecom/conference/detail/9/
> 'tuple' object has no attribute 'strftime'
> ----
> This happens on update of newform. What am I doing wrong?
>
> view code:
> -----------------
[...]
> conf.conference_date=form.clean_data['conference_date'],

In python if you do an assignment where the right hand side has one or
more commas then it means you are creating a tuple:

>>> a = 1
>>> a
1
>>> a = 1,
>>> a
(1,)

So just remove the trailing commas in your view and you will then be
assigning a date rather than a tuple to conference_date.

In general the error "'tuple' object has no attribute xxx" normally
means a trialling comma slipped in somewhere - it is a mistake I have
made several times.

--
Phil

Reply all
Reply to author
Forward
0 new messages