fedora 7
django 0.96
DATABASE_ENGINE = 'sqlite3'
maybe its late but DateField is causing me some grief when trying to
load some test data from
the command line (ie: not via admin web).
in model
test_date = models.DateField()
in another python script (run on command line ) that imports the model
and attempts to populate an sqlite
database with lots of entries (test data from ascii file , including
date fields in the format yyyy-mm-dd)
eg: equivalent to
obj.test_date='2007-12-04'
obj.save()
it complains about
raceback (most recent call last):
File "populate_database.py", line 136, in <module>
tr.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py", line
223, in save
db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in
self._meta.fields if not isinstance(f, AutoField)]
File
"/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py",
line 494, in get_db_prep_save
value = value.strftime('%Y-%m-%d')
AttributeError: 'str' object has no attribute 'strftime'
SO, thought I would have to do a
time.strptime('2007-02-23', '%Y-%m-%d') thing in between but did not get
it working.
Suggestions welcome :-)
/F
Hi Frank,
You're the second person in recent history to report this problem -
however, I've been unable to replicate it. The last user was on
Windows, and I had mentally put it down as a configuration issue; the
fact that you're seeing it on Linux suggests that it might be a larger
problem.
The short version: The database backend (sqlite/pysqlite) should be
returning datetime objects for records containing date fields, but for
some reason, it seems to be returning strings for some users.
If you can provide a simple test case that exhibits the problem (e.g.,
minimal model, set of instructions for generating the error, complete
set of version details for OS, database, etc), I'll have another look
and see what I can see.
Yours,
Russ Magee %-)
class Image(Model):
name = CharField(maxlength=255)
owner = ForeignKey('user')
file = ImageField(upload_to='Image/%Y-%m/', blank=True, null=True)
description = TextField()
datetime = DateTimeField(auto_now_add=True)
class Admin:
search_fields = ('description','name')
list_filter = ('datetime',)
def __str__(self): return self.name
Add the app to my installed apps list and then any attempt to run
manage.py results in:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/lib/python2.5/site-packages/django/core/management.py",
line 1725, in execute_manager
execute_from_command_line(action_mapping, argv)
File "/usr/lib/python2.5/site-packages/django/core/management.py",
line 1616, in execute_from_command_line
action_mapping[action](int(options.verbosity),
options.interactive)
File "/usr/lib/python2.5/site-packages/django/core/management.py",
line 510, in syncdb
_check_for_validation_errors()
File "/usr/lib/python2.5/site-packages/django/core/management.py",
line 1195, in _check_for_validation_errors
num_errors = get_validation_errors(s, app)
File "/usr/lib/python2.5/site-packages/django/core/management.py",
line 1046, in get_validation_errors
for r in rel_opts.get_all_related_objects():
File "/usr/lib/python2.5/site-packages/django/db/models/options.py",
line 146, in get_all_related_objects
if f.rel and self == f.rel.to._meta:
AttributeError: 'str' object has no attribute '_meta'
Seems to be only a problem with ImageField, FileField still works
Fedora Core 7
Django (0, 97, 'pre') (SVN)
Python 2.5
MySql 5.0.37
On Aug 5, 8:27 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:
This problem is unrelated to Image or FileFields - the problem is your
ForeignKey.
String-form foreign key references only work with models defined in
the same application. If you want to reference the User model, you
will need to add
from django.contrib.auth.models import User
to the top of your file, and change your ForeignKey reference to:
owner = ForeignKey(User)
Yours,
Russ Magee %-)
DateField/TimeField/DateTimeField need the python objects for them,
not strings.
They convert it to a string internally before save, so that the
database gets the right value, but by keeping it the python objects
outside of that, you don't have to do a lot of string processing if
you need to change a date :)
<looks closer at example>
<slaps forehead>
You are completely correct. I saw the error message, saw it was
similar to a problem I was triaging recently, and didn't look close
enough at the details.
Apologies to Frank for leading you astray on this one.
Yours,
Russ Magee %-)
>This is not a backend issue at all - the problem is you're assigning a
>string to the DateField, which is not what it needs.
>
>
Hi Keith and Collin,
This is what happens when you try to code at 1:30am :-)
Yes I have it working ok
inputdata is a split line read from a file, with the date in a field
position specified by MY_DATE
eg:
mydate = datetime.date(*time.strptime(inputdata[MY_DATE], '%Y-%m-%d')[:3])
I'll take some stronger tea next time !!
/Frank
>On 8/8/07, Collin Grady <cgr...@gmail.com> wrote:
>
>
>>This is not a backend issue at all - the problem is you're assigning a
>>string to the DateField, which is not what it needs.
>>
>>
Thank Russ for the help.. typo on my part ;-)
yes this works !!
mydate = datetime.date(*time.strptime(inputdata[MY_DATE], '%Y-%m-%d')[:3])
Cheers.
/Frank