gpt4 book ai didi

python - 尝试播种 YAML 数据时出现 "value must be an integer"

转载 作者:行者123 更新时间:2023-12-01 01:39:12 26 4
gpt4 key购买 nike

我正在使用 Django 2、Python 3.7 和 django-address 模块 ( https://pypi.org/project/django-address/ )。我正在尝试插入一些种子数据。我有这个 YAML ...

- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state:
name: IL
country:
- United States

当我运行我的种子命令时
python manage.py loaddata maps/fixtures/seed_data.yaml

我收到这个错误...
localhost:web davea$ python manage.py loaddata maps/fixtures/seed_data.yaml
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 923, in to_python
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 157, in Deserializer
data[field.attname] = model._meta.get_field(field_name).to_python(field_value)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 928, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'{'name': 'IL', 'country': ['United States']}' value must be an integer."]

During handling of the above exception, another exception occurred:

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 "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
self.load_label(fixture_label)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
for obj in objects:
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 159, in Deserializer
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/davea/Documents/workspace/chicommons/maps/web/maps/fixtures/seed_data.yaml': ["'{'name': 'IL', 'country': ['United States']}' value must be an integer."]: (address.locality:pk=1) field_value was '{'name': 'IL', 'country': ['United States']}'

我已将其添加到文件 maps/monkey_patching.py 中,以帮助基于唯一标识符自动创建实体...
from address import State
from address import Country

def country_get_by_natural_key(self, name):
return self.get_or_create(name=name)[0]

def state_get_by_natural_key(self, name, country_id):
return self.get_or_create(name=name, country_id=country_id)[0]

Country.add_to_class("get_by_natural_key",country_get_by_natural_key)
State.add_to_class("get_by_natural_key",state_get_by_natural_key)

编辑:包括每个 Shivam 请求的堆栈跟踪...
(venv) localhost:maps davea$ python web/manage.py loaddata web/maps/fixtures/seed_data.yaml 
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 923, in to_python
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 157, in Deserializer
data[field.attname] = model._meta.get_field(field_name).to_python(field_value)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 928, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'['IL', 'United States']' value must be an integer."]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "web/manage.py", line 21, in <module>
main()
File "web/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
self.load_label(fixture_label)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
for obj in objects:
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/serializers/python.py", line 159, in Deserializer
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/davea/Documents/workspace/chicommons/maps/web/maps/fixtures/seed_data.yaml': ["'['IL', 'United States']' value must be an integer."]: (address.locality:pk=1) field_value was '['IL', 'United States']'

最佳答案

自然键问题
get_by_natural_key方法在经理级别可用,但您在模型级别添加方法。因此,您需要这样做,而不是上面的内容:

from django.db import models
from address.models import State

class CustomManager(models.Manager):
def get_by_natural_key(self, name):
return self.get_or_create(name=name)[0]

State.add_to_class('objects', CustomManager())

夹具问题

固定装置的嵌套数据应该只包含 args 而不是 kwargs。查看 Is there a way in a seed_data.yaml file to autogenerate models on which first model is dependent upon?了解更多详情。之后,您需要设计您的 get_by_natural_key相应地发挥作用。所以对于 locality模型夹具看起来像
- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state: ['I', 'United States']

而被覆盖的经理将是
class CustomManager(models.Manager):
def get_by_natural_key(self, state_name, country):
country = Country.objects.get_or_create(name=country)[0]
return State.objects.get_or_create(name=state_name, country=country)[0]

setattr(State._meta, 'default_manager', CustomManager())

关于python - 尝试播种 YAML 数据时出现 "value must be an integer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60102585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com