Opened 9 years ago

Closed 8 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: master
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: UI/UX:

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@…> 8 years ago.
unicode_strftime_fix.2.diff (530 bytes) - added by deepak <deep.thukral@…> 8 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 9 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Accepted

I could reproduce it.

comment:2 Changed 8 years ago by deepak <deep.thukral@…>

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

comment:3 Changed 8 years ago by Ikipou

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'

Changed 8 years ago by deepak <deep.thukral@…>

comment:4 Changed 8 years ago by deepak <deep.thukral@…>

  • 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 Changed 8 years ago by Simon G. <dev@…>

  • Needs tests unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 8 years ago by mtredinnick

  • Triage Stage changed from Ready for checkin to Accepted

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 Changed 8 years ago by mtredinnick

  • Patch needs improvement set

Changed 8 years ago by deepak <deep.thukral@…>

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

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 8 years ago by jacob

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

(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