繁体   English   中英

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

[英]django : loading fixtures with natural foreignkey fails with 'ValueError: invalid literal for int() with base 10'

我的模特是......

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,
        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,)


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',
        max_length=100,
        help_text='''
        100 chars max
        '''
    )
    code = models.CharField(
        'Code',
        max_length=10,
        help_text='''
        10 chars max
        ''',
        null=True, 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",
        "state": [
            "Andaman and Nicobar"
        ],
        "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, **kwargs)
  File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", 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", 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, **kwargs)
  File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/parijath/Projects/virtualenv/django18/lib/python3.4/site-packages/django/db/models/query.py", 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)
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, name, state):
        return self.get(name=name, state=state)

我已将代码修改为......

class DistrictManager(models.Manager):
    def get_by_natural_key(self, name, state):
        return self.get(name=name, state__name=state)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM