我有以下模特和经理。
class StateManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class State(models.Model):
class Meta:
verbose_name = "State"
verbose_name_plural = "States"
permissions = (
('access_state', 'Can access States'),
)
COUNTRIES = (
('India', 'India'),
('USA', 'USA'),
('Thailand', 'Thailand'),
)
# Managers
objects = StateManager()
# Database fields
name = models.CharField(
'Name',
max_length=100,
unique=True,
help_text='''
100 chars max
'''
)
code = models.CharField(
'Code',
max_length=10,
unique=True,
help_text='''
10 chars max
''',
null=True, blank=True
)
country = models.CharField(
max_length=50,
default="India",
choices=COUNTRIES,
blank=False,
null=False
)
def __str__(self):
return self.name
def natural_key(self):
return self.name
我的夹具文件在下面给出
[
{
"model": "parties.state",
"fields": {
"name": "Andaman and Nicobar",
"code": "AN",
"country": "India"
}
},
{
"model": "parties.state",
"fields": {
"name": "Andhra Pradesh",
"code": "AP",
"country": "India"
}
},
]
我之前已将数据转储到fixture文件中。但是当我现在尝试加载夹具时,我收到以下错误......
Traceback (most recent call last):
.....
.....
TypeError: get_by_natural_key() takes 2 positional arguments but 20 were given
.....
.....
File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/base.py", line 195, in build_instance
obj.pk = Model._default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/parijath/Projects/django_projects/webportal18_multipleapps/parties/fixtures/states.json': get_by_natural_key() takes 2 positional arguments but 20 were given
答案 0 :(得分:3)
php curl方法应返回元组,而不是字符串。
save()
如果def natural_key(self):
return (self.name,)
是字符串natural_key
而不是元组"Andaman and Nicobar"
,则('Andaman and Nicobar',)
会将字符串中的19个字符中的每一个解包为单独的参数。与*natural_key
一起,它会从您的错误消息中提供20个参数。