前提・実現したいこと
ポケモン図鑑のようなものを作っています。
後々Django rest frameworksでAPIを作成する予定です。
その際、初期データを
Python
1python manage.py loaddata post_initial.json
で投入したいと考えておりますが、どう改善すればよいかわからず難儀しております。
「local variable 'pk' referenced before assignment」などとありますが、どこを修正すればよいかわかりかねております。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\serializers\base.py", line 287, in deserialize_m2m_values for pk in field_value: TypeError: 'int' object is not iterable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\serializers\json.py", line 69, in Deserializer yield from PythonDeserializer(objects, **options) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\serializers\python.py", line 122, in Deserializer values = base.deserialize_m2m_values(field, field_value, using, handle_forward_references) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\serializers\base.py", line 294, in deserialize_m2m_values raise M2MDeserializationError(e, pk) UnboundLocalError: local variable 'pk' referenced before assignment The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line utility.execute() File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 369, in execute output = self.handle(*args, **options) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\loaddata.py", line 72, in handle self.loaddata(fixture_labels) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\loaddata.py", line 114, in loaddata self.load_label(fixture_label) File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\loaddata.py", line 172, in load_label for obj in objects: File "C:\Users[USERNAME]\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\serializers\json.py", line 73, in Deserializer raise DeserializationError() from exc django.core.serializers.base.DeserializationError: Problem installing fixture 'C:\Users[USERNAME]\Documents[*****]\django_rest_framework_test\Pokedex\fixtures\post_initial.json':
該当のソースコード
model.py
from django.db import models class Pokemon(models.Model): name= models.CharField(max_length=32,primary_key = True) type= models.ManyToManyField( "Type", ) class Type(models.Model): id = models.AutoField(primary_key = True) name=models.CharField(max_length=32)
post_initial.json
[ { "model": "Pokedex.Pokemon", "pk": 1, "fields": { "name": "ヒトカゲ", "type":1 } }, { "model": "Pokedex.Pokemon", "pk": 2, "fields": { "name": "フシギダネ", "type":2 } }, { "model": "Pokedex.Pokemon", "pk": 3, "fields": { "name": "ゼニガメ", "type":3 } }, { "model": "Pokedex.Pokemon", "pk": 4, "fields": { "name": "リザードン", "type":[1,5] } }, { "model": "Pokedex.Pokemon", "pk": 5, "fields": { "name": "フシギバナ", "type":[1,4] } }, { "model": "Pokedex.Pokemon", "pk": 6, "fields": { "name": "カメックス", "type":3 } }, { "model": "Pokedex.Type", "pk": 1, "fields": { "name": "ほのお" } }, { "model": "Pokedex.Type", "pk": 2, "fields": { "name": "くさ" } }, { "model": "Pokedex.Type", "pk": 3, "fields": { "name": "みず" } }, { "model": "Pokedex.Type", "pk": 4, "fields": { "name": "どく" } }, { "model": "Pokedex.Type", "pk": 5, "fields": { "name": "ひこう" } } ]
どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/14 07:20 編集