我是一个相对的 django 新手。我有一个来自现有数据库的 django.contrib.auth.user 对象的 json 固定装置,我想将它加载到我的测试应用程序中。我要修改的特定字段是所有没有内置 UTC 偏移量的日期时间字段。我想使用 python 脚本添加这些 UTC 偏移量。
我正在使用 django 反序列化器,但没有运气,并且在反序列化过程中出现错误。
File "add_utc_offset_to_fixture.py", line 24, in <module>
for obj in serializers.deserialize("json", json_fixture):
File "/Users/hari/.virtualenvs/bsc2/lib/python2.7/site-packages/django/core/serializers/json.py", line 47, in Deserializer
raise DeserializationError(e)
django.core.serializers.base.DeserializationError: No module named Image
如何解决此反序列化错误,或者如何在加载到数据库之前修改此固定装置。
我查看了我的固定装置以及 json.py 反序列化器,但不明白为什么它需要一个名为 Image 的模块。
我的代码
# This program reads in a json fixture with naive Datetime field and converts it into a UTC aware Datetime field
# For Documentation on this see https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#time-zones-migration-guide
import sys,os
# Sets the django settings module
dir_two_steps_up_from_me = os.path.join(os.path.split(os.path.dirname(os.path.abspath(__file__)))[-2])
print "Adding %s to sys.path" % dir_two_steps_up_from_me
sys.path.append(dir_two_steps_up_from_me)
from bsc2 import settings
from django.core.management import setup_environ
# Deprecated but still using
setup_environ(settings)
from django.core import serializers
from django.contrib.auth.models import User
json_fixture = None
try:
json_fixture = open(sys.argv[1],"rb")
except IndexError,IOError:
print "Please give json fixture"
exit()
for obj in serializers.deserialize("json", json_fixture):
# Getting deserialization error when this executes
print obj.first_name
# TODO Code to change naive time in last_login to UTC time