我有一个包含多个模型的项目,所有这些模型都在实现自然键。其中大多数都在工作,但是当我使用自然键导出时,有一个模型(到目前为止)拒绝加载。
注意:如果我在导出时不使用自然键,它可以工作。
不会加载的模型是CampaignTransaction, Campaign 有效。
这是我的模型和经理:
class CampainManager(models.Manager):
def get_by_natural_key(self, title, org):
return self.get(title=title, org__name=org)
class Campaign(models.Model):
"""Donation Campaign information
**Referenced By:** :class:`CampaignTransaction`"""
title = models.CharField(max_length=100)
"""Campaign Title"""
org = models.ForeignKey(Organization, on_delete=models.PROTECT)
""":class:`~FundingHub.common.models.Organization` conducting campaign"""
description = models.TextField()
"""Description of campaign"""
start_date = models.DateTimeField()
"""Start date of campaign"""
end_date = models.DateTimeField()
"""End date of campaign"""
objects = CampainManager()
def __str__(self):
return self.title
def natural_key(self):
return (self.title, self.org.natural_key())
natural_key.dependencies = ['common.Organization']
class Meta:
unique_together = ('org', 'title')
class CampaignTransactionManager(models.Manager):
def get_by_natural_key(self, cc_type, trans_id, campaign):
return self.get(cc_type=cc_type, cc_transaction_id=trans_id, campaign__name=campaign)
class CampaignTransaction(models.Model):
"""A single donation"""
campaign = models.ForeignKey(Campaign, on_delete=models.PROTECT)
"""The :class:`Campaign` that is being donated to"""
user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT)
"""The :class:`~FundingHub.common.models.User` that is making the donation"""
amount = models.DecimalField(max_digits=9, decimal_places=2)
"""Amount of donation"""
cc_type = models.CharField(max_length=3, choices=CC_TYPES)
"""Type of credi card used to make the donation :data:`~FundingHub.common.models.CC_TYPES` contains the valid options"""
cc_transaction_id = models.CharField(max_length=200)
"""Credit card transaction ID"""
trans_date = models.DateTimeField()
"""Date and time of transaction"""
objects = CampaignTransactionManager()
def __str__(self):
return F'{self.campaign.title} - {self.cc_transaction_id} - {self.amount}'
def natural_key(self):
return (self.cc_type, self.cc_transaction_id, self.campaign.natural_key())
natural_key.dependencies = ['donations.Campaign']
class Meta:
unique_together = ('cc_type', 'cc_transaction_id')
这是输出python manage.py dumpdata donations --database test --indent=4 --natural-foreign > xx.json
:
[
{
"model": "donations.campaign",
"pk": 1,
"fields": {
"title": "Taco Tuesday",
"org": [
"Tacos For Freedom"
],
"description": "Tacos on Tuesday",
"start_date": "2020-05-28T18:08:13Z",
"end_date": "2020-05-28T22:00:00Z"
}
},
{
"model": "donations.campaigntransaction",
"pk": 1,
"fields": {
"campaign": [
"Taco Tuesday",
[
"Tacos For Freedom"
]
],
"user": [
"cc@hotmail.com"
],
"amount": "1000.00",
"cc_type": "VIS",
"cc_transaction_id": "VIS-1234567890",
"trans_date": "2020-05-28T19:33:48Z"
}
}
]
然后我尝试使用python manage.py loaddata --database test xx.json
我收到错误重新加载它:
Traceback (most recent call last):
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: donations_campaigntransaction.campaign_id
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 "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
self.loaddata(fixture_labels)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
self.load_label(fixture_label)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/management/commands/loaddata.py", line 181, in load_label
obj.save(using=self.using)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/core/serializers/base.py", line 223, in save
models.Model.save_base(self.object, using=using, raw=True, **kwargs)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/base.py", line 784, in save_base
force_update, using, update_fields,
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/base.py", line 865, in _save_table
forced_update)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/base.py", line 917, in _do_update
return filtered._update(values) > 0
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/query.py", line 771, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1499, in execute_sql
cursor = super().execute_sql(result_type)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
cursor.execute(sql, params)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Users/alexthomas/.venvs/FHEnv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: Problem installing fixture '/Users/alexthomas/Projects/FundingHub/xx.json': Could not load donations.CampaignTransaction(pk=1): NOT NULL constraint failed: donations_campaigntransaction.campaign_id
它似乎在抱怨空值,但我看不到缺少什么。从夹具。