私は相対的なジャンゴの初心者です。テストアプリにロードしたい既存のデータベースの 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