python – django:使用自然外键加载fixture失败,出现’ValueError:int(无效的文字,基数为10)

python – django:使用自然外键加载fixture失败,出现’ValueError:int(无效的文字,基数为10),第1张

概述我的模特是…… class StateManager(models.Manager): def get_by_natural_key(self, name): return self.get(name=name)class DistrictManager(models.Manager): def get_by_natural_key(self, name, s 我的模特是……

class StateManager(models.Manager):    def get_by_natural_key(self,name):        return self.get(name=name)class districtManager(models.Manager):    def get_by_natural_key(self,name,state):        return self.get(name=name,state=state)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,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,)class district(models.Model):    class Meta:        verbose_name = "district"        verbose_name_plural = "districts"        unique_together = (            (                "state","name"            ),)    # Managers    objects = districtManager()    # Database fIElds    name = models.CharFIEld(        'name',blank=True    )    state = models.ForeignKey(State)    def __str__(self):        return self.name    def natural_key(self):        return (self.name,) + self.state.natural_key()    natural_key.dependencIEs = ['partIEs.state']

我对区模型的夹具是……

[{    "model": "partIEs.district","fIElds": {        "name": "north Andaman","state": [            "Andaman and Nicobar"        ],"code": "NA"    }},{    "model": "partIEs.district","fIElds": {        "name": "South Andaman","code": "SA"    }}]

实际上,夹具是由django的’dumpdata’本身生成的.

但是,在尝试加载夹具时,我收到以下错误…

ValueError: invalID literal for int() with base 10: 'Andaman and Nicobar'The full trace is given below ...Traceback (most recent call last):  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/Json.py",line 79,in Deserializer    for obj in PythonDeserializer(objects,**options):  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/python.py",line 157,in Deserializer    obj = base.build_instance(Model,data,db)  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  file "/home/parijath/Projects/django_projects/webportal18_multipleapps/partIEs/models.py",line 17,in get_by_natural_key    return self.get(name=name,state=state)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/manager.py",line 127,in manager_method    return getattr(self.get_queryset(),name)(*args,**kwargs)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py",line 325,in get    clone = self.filter(*args,line 679,in filter    return self._filter_or_exclude(False,*args,line 697,in _filter_or_exclude    clone.query.add_q(Q(*args,**kwargs))  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py",line 1310,in add_q    clause,require_inner = self._add_q(where_part,self.used_aliases)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py",line 1338,in _add_q    allow_joins=allow_joins,split_subq=split_subq,file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/sql/query.py",line 1200,in build_filter    lookups,value)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fIElds/related.py",line 1761,in get_lookup_constraint    lookup_class(target.get_col(alias,source),val),AND)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py",line 101,in __init__    self.rhs = self.get_prep_lookup()  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/lookups.py",line 139,in get_prep_lookup    return self.lhs.output_fIEld.get_prep_lookup(self.lookup_name,self.rhs)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fIElds/__init__.py",line 727,in get_prep_lookup    return self.get_prep_value(value)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/fIElds/__init__.py",line 985,in get_prep_value    return int(value)ValueError: invalID literal for int() with base 10: 'Andaman and Nicobar'During handling of the above exception,another exception occurred:Traceback (most recent call last):  file "manage.py",line 10,in <module>    execute_from_command_line(sys.argv)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/__init__.py",line 354,in execute_from_command_line    utility.execute()  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/__init__.py",line 346,in execute    self.fetch_command(subcommand).run_from_argv(self.argv)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/base.py",line 394,in run_from_argv    self.execute(*args,**cmd_options)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/base.py",line 445,in execute    output = self.handle(*args,**options)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py",line 60,in handle    self.loaddata(fixture_labels)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py",line 100,in loaddata    self.load_label(fixture_label)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/management/commands/loaddata.py",line 151,in load_label    for obj in objects:  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/Json.py",line 85,in Deserializer    six.reraise(DeserializationError,DeserializationError(e),sys.exc_info()[2])  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/utils/six.py",line 685,in reraise    raise value.with_traceback(tb)  file "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/core/serializers/Json.py",in get_prep_value    return int(value)django.core.serializers.base.DeserializationError: Problem installing fixture '/home/parijath/Projects/django_projects/webportal18_multipleapps/partIEs/fixtures/districts-2.Json': invalID literal for int() with base 10: 'Andaman and Nicobar'

我哪里做错了?

解决方法 最后,我发现了我正在做的错误.

代替 ….

class districtManager(models.Manager):    def get_by_natural_key(self,state=state)

我已将代码修改为……

class districtManager(models.Manager):    def get_by_natural_key(self,state__name=state)

这里的要点是state__name = state(not state = state),我之前错过了.

总结

以上是内存溢出为你收集整理的python – django:使用自然外键加载fixture失败,出现’ValueError:int(无效的文字,基数为10)全部内容,希望文章能够帮你解决python – django:使用自然外键加载fixture失败,出现’ValueError:int(无效的文字,基数为10)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1196040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存