Getting error: 'unicode' object has no attribute 'strftime'

1,127 views
Skip to first unread message

Greg

unread,
Oct 12, 2007, 6:34:54 PM10/12/07
to Django users
Hello,
I just added the following field to my Style class:

newrugs = models.DateField(auto_now_add=True)

Now, when I try to view my Style objects in the admin I get the
following error:

AttributeError at /admin/plush/style/5/
'unicode' object has no attribute 'strftime'

////////////////////

Any suggestions on what's causing this?

Greg

unread,
Oct 13, 2007, 10:35:00 AM10/13/07
to Django users
Ok,
I've made some changes and now I'm able to add styles in the admin.
However, it required me to change change a django file that I'm not
sure I should change. Here is what i changed.

In c:\Python24\lib\site-packages\django\db\models\fields\__init__.py
and changed the method 'flatten_data(self, follow, obj=None):'. I
changed the following line of code from:

return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or
'')}

to

return {self.attname: (val is not None or '')}

/////////

That seems to fix the problem. Is the code that I took needed there
for a reason? Will this changed have an effect on how other areas of
my application work?

Thanks

Karen Tracey

unread,
Oct 13, 2007, 11:59:28 AM10/13/07
to django...@googlegroups.com
At 10:35 AM 10/13/2007, Greg wrote:
[snip details of change to django code]

>That seems to fix the problem. Is the code that I took needed there
>for a reason? Will this changed have an effect on how other areas of
>my application work?

Yes, that code is needed and is there for a reason. The object
passed to that function should NOT be a unicode object, it ought to
be a datetime object so that strftime can be called on it to properly
format it. The bug is not the line of code you changed, it is
whatever is causing a unicode object to be passed into the
flatten_data function.

I see from the archives you have brought this problem to the group
before, but I don't see any resolution. I gather you are using
sqlite, which, I'm sorry, I know nothing about so I can't really help
you. For some reason you are getting back a unicode object from the
db instead of a datetime. You say you just added this DateTime field
to your model. Did you modify the table in sqlite yourself or did
you re-initialize using django? It seems that either your version of
sqlite is behaving oddly and returning a unicode string for something
that ought to be a datetime object, or the field is not of the proper
type in the database.

Karen

entropi...@gmail.com

unread,
Oct 15, 2007, 9:51:54 AM10/15/07
to Django users
I had a similar problem in one of my models when I was trying to
display a date as a string (had joined a date and a string in a model
function). From what I recall (not certain), unicode() on the date
fixed the problem.
Michael
Reply all
Reply to author
Forward
0 new messages