我正在使用Django 2.0,Python 3.7和MySql5。我有以下两个模型,第二个依赖于第一个模型...
class CoopType(models.Model):
name = models.CharField(max_length=200, null=False)
class Meta:
unique_together = ("name",)
class Coop(models.Model):
name = models.CharField(max_length=250, null=False)
type = models.ForeignKey(CoopType, on_delete=None)
address = AddressField(on_delete=models.CASCADE)
enabled = models.BooleanField(default=True, null=False)
phone = PhoneNumberField(null=True)
email = models.EmailField(null=True)
web_site = models.TextField()
我正在为第二个模型创建一些种子数据。我想知道是否有一种方法可以从第二个模型中自动为第一个模型生成数据。我试过了...
- model: maps.coop
pk: 1
fields:
name: "1871"
type:
pk: null
name: Coworking Space
address:
street_number: 222
route: 1212
raw: 222 W. Merchandise Mart Plaza, Suite 1212
formatted: 222 W. Merchandise Mart Plaza, Suite 1212
locality:
name: Chicago
postal_code: 60654
state:
code: IL
enabled: True
phone:
email:
web_site: "http://www.1871.com/"
latitude: 41.88802611
longitude: -87.63612199
但是运行种子数据时出现此错误...
(env) localhost:maps davea$ python scripts/parse_coop_csv.py ~/Downloads/chicommons_prep.xlsx\ -\ Mapping\ Sheet.csv > maps/fixtures/seed_data.yaml
(env) localhost:maps davea$ python manage.py loaddata maps/fixtures/seed_data.yaml
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/env/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/env/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/env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 928, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["'{'pk': None, 'name': 'Coworking Space'}' 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/env/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/env/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/env/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/env/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/env/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/env/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/env/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/env/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/env/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/maps/maps/fixtures/seed_data.yaml': ["'{'pk': None, 'name': 'Coworking Space'}' value must be an integer."]: (maps.coop:pk=1) field_value was '{'pk': None, 'name': 'Coworking Space'}'
seed_data.yaml文件中有什么方法可以优雅地做到这一点吗?
首先,给定的[.yaml
文件结构为错误。种子数据/转储数据如下所示,
- model: map.coop
pk: 1
fields:
type: 1
address: 5
enabled: true
phone: '917894561230'
email: '[email protected]'
web_site: ''
- model: map.coop
pk: 2
fields:
type: 1
address: 5
enabled: true
phone: '917894513230'
email: null
web_site: ''
- model: map.coop
pk: 3
fields:
type: 6
address: 3
enabled: false
phone: '917894585230'
email: '[email protected]'
web_site: 'http://foobar.com'
[当您正在处理由相关字段组成的种子数据,如FK时,应注意serialization of Natural Keys。除此之外,从the doc of Django 2.0
由于自然键依赖数据库查找来解析引用,因此在引用数据之前先存在数据很重要。您无法使用自然键进行“前向引用”-您要引用的数据必须先存在,然后才能包含对数据的自然键引用。