我是django的新手,我仍然对文档中写的东西感到困惑。 我使用django 1.8和python 2。 阅读本段about saving an object in db,我对此感到困惑:
例如,DateField字段使用Python日期时间对象进行存储 数据。数据库不存储日期时间对象,因此字段值必须 被转换为符合ISO标准的日期字符串,用于插入 数据库中。
在线我发现了非常不同的方法,但实际上我真的不明白如何转换符合ISO标准的日期字符串!
此外,在我的数据库中,我有DateTimeField,DateField,TimeField ......如何进行转换以在我的数据库中保存新对象?
谢谢!
编辑: 模型
class CommunityList (models.Model):
id_community = models.UUIDField (primary_key=True)
data_creation = models.DateField
subcommunity_flag = models.BooleanField
我从文件导入数据。 这是完整的追溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/Community/CommunityApp/CommunityList
Django Version: 1.8
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CommunityApp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/DjangoProjects/Community/CommunityApp/views.py" in import_data
108. CommunityList(datareader)
File "/home/DjangoProjects/Community/CommunityApp/views.py" in CommunityList
80. subcommunity_flag=row['subcommunity_flag']
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in __init__
480. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
Exception Type: TypeError at /Community/CommunityApp/CommunityList
Exception Value: 'data_creation' is an invalid keyword argument for this function
答案 0 :(得分:1)
答案 1 :(得分:0)
您的问题与字段转换没有任何关系。
您的模型实际上并不包含data_creation
或subcommunity_flag
字段,因为您没有在模型定义中调用字段类。
data_creation = models.DateField()
subcommunity_flag = models.BooleanField()