Opened 18 years ago

Closed 17 years ago

#3146 closed defect (fixed)

'str' object has no attribute 'strftime' Error when the foreign key is a date

Reported by: Ikipou@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: normal Keywords: date foreign key
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When a model use a date as foreign key, the admin interface crash when creating or editing an object.
For exemple, the following model crash when creating Event in the admin interface:

class Day(models.Model):
    date = models.DateField(primary_key=True, db_column='Date')

    class Admin:
        pass

class Event(models.Model):
    id_event = models.AutoField(primary_key=True, db_column='idEvent')
    date = models.ForeignKey(JourneeElectorale, db_column='Date')

    class Admin:
        pass

Complete error is :
Exception Type: AttributeError
Exception Value: 'str' object has no attribute 'strftime'
Exception Location: /usr/lib/python2.4/site-packages/django/db/models/fields/init.py in get_db_prep_save, line 467

Attachments (2)

unicode_strftime_fix.diff (479 bytes ) - added by deepak <deep.thukral@…> 17 years ago.
unicode_strftime_fix.2.diff (530 bytes ) - added by deepak <deep.thukral@…> 17 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by Michael Radziej <mir@…>, 18 years ago

Triage Stage: UnreviewedAccepted

I could reproduce it.

comment:2 by deepak <deep.thukral@…>, 17 years ago

It'd be nice if you could post complete Tracback.

comment:3 by Ikipou, 17 years ago

The Traceback with the last SVN version and Python 2.5:

Traceback (most recent call last):
File "/usr/local/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response

  1. response = callback(request, *callback_args, callback_kwargs)

File "/usr/local/lib/python2.5/site-packages/django/contrib/admin/views/decorators.py" in _checklogin

  1. return view_func(request, *args, kwargs)

File "/usr/local/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func

  1. response = view_func(request, *args, kwargs)

File "/usr/local/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in add_stage

  1. new_object = manipulator.save(new_data)

File "/usr/local/lib/python2.5/site-packages/django/db/models/manipulators.py" in save

  1. new_object.save()

File "/usr/local/lib/python2.5/site-packages/django/db/models/base.py" in save

  1. db_values = [f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]

File "/usr/local/lib/python2.5/site-packages/django/db/models/fields/related.py" in get_db_prep_save

  1. return self.rel.get_related_field().get_db_prep_save(value)

File "/usr/local/lib/python2.5/site-packages/django/db/models/fields/init.py" in get_db_prep_save

  1. value = value.strftime('%Y-%m-%d')

AttributeError at /admin/foreigndate/event/add/
'unicode' object has no attribute 'strftime'

by deepak <deep.thukral@…>, 17 years ago

Attachment: unicode_strftime_fix.diff added

comment:4 by deepak <deep.thukral@…>, 17 years ago

Has patch: set
Needs tests: set

I could reproduce it. I added a patch, not intensively tested but working for me. I think this problem arose due to unicode fixes.

comment:5 by Simon G. <dev@…>, 17 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Malcolm Tredinnick, 17 years ago

Triage Stage: Ready for checkinAccepted

What directory is the file being patched in? There are many files called __init__.py in the tree. Plesae regenerate the patch from the top of the source tree.

comment:7 by Malcolm Tredinnick, 17 years ago

Patch needs improvement: set

by deepak <deep.thukral@…>, 17 years ago

Attachment: unicode_strftime_fix.2.diff added

comment:8 by deepak <deep.thukral@…>, 17 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Jacob, 17 years ago

Resolution: fixed
Status: newclosed

(In [6193]) Fixed #3146: DateFields no longer barf when confronted by strings. Thanks, Deepak Thukral.

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