问题
I'm using Django 2, Python 3.7 and the django-address module (https://pypi.org/project/django-address/). I'm trying to insert some seed data. I have this YAML ...
- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state:
name: IL
country:
- United States
When I run my seed command
python manage.py loaddata maps/fixtures/seed_data.yaml
I get this error ...
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']}'
I have added this in the file maps/monkey_patching.py to help with auto-creating the entities based on unique identifiers ...
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)
Edit: including stack trace per Shivam's request ...
(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']'
回答1:
Issue with natural key
get_by_natural_key
method is available on the manager level but you are adding method on model level. So instead of above, you need to do like this:
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())
Issue with fixtures
Nested data of fixtures should only contain args and not kwargs. Check Is there a way in a seed_data.yaml file to autogenerate models on which first model is dependent upon? for more detail. And after that, you need to design your get_by_natural_key
function accordingly. So for locality
model fixtures would look like
- model: address.locality
pk: 1
fields:
name: "Chicago"
postal_code: "60053"
state: ['I', 'United States']
And overrided manager would be
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())
回答2:
I would offer an alternative approach, which is just to write your own management command to import this data, instead of trying to make loaddata
work - I think this is potentially much less effort.
This command works for the sample data you've provided - it may need some minor adjustments if you have more than just address.locality
objects in your YAML file:
import yaml
from django.core.management.base import BaseCommand
from address.models import Country, State, Locality
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('file', nargs=1, type=str)
def handle(self, *args, **options):
f = options['file'][0]
with open(f, 'r') as yamlfile:
data = yaml.load(yamlfile)
for row in data:
if row['model'] == 'address.locality':
state_name = row['fields']['state']['name']
country_name = row['fields']['state']['country'][0]
country_obj, _ = Country.objects.get_or_create(name=country_name)
state_obj, _ = State.objects.get_or_create(country=country_obj, name=state_name)
Locality.objects.get_or_create(
pk=row['pk'],
state=state_obj,
postal_code=row['fields']['postal_code'],
name=row['fields']['name']
)
(Side note - the format of your YAML is a little weird, as it provides a list for country
. I would have thought a state can only belong to a single country?).
回答3:
In the source code of this package, Locality.state
field is related to State
model by a foreignKey
. So, your data fixture state
field must be an integer of the related table name.
See django-address source code
PS: In your case you must build all the related models that Locality
model is related to. If not you'll have a data integrity error ... And all bunch of other errors.
来源:https://stackoverflow.com/questions/60102585/getting-value-must-be-an-integer-when-trying-to-seed-yaml-data