- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的模型是......
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']
我的 District 模型夹具是......
[
{
"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"
}
}
]
事实上,fixture 是由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
(不是 state=state
),我之前错过了。
关于python - Django : loading fixtures with natural foreignkey fails with 'ValueError: invalid literal for int() with base 10' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655824/
我有一个关于使用的类型的性质的理论问题 很多解释 Coyoneda 引理的例子。它们通常被称为 作为“自然变换”,据我所知,仿函数之间的映射。 令我困惑的是,在这些示例中,它们有时映射自 Set。到某
这是一个小设计,在自然键上具有常见的 NOT NULL UNIQUE 约束: CREATE TABLE 'users' { id int(10) NOT NULL AUTO_INCREMENT
下面是代码 postgres=# SELECT title,name,borrower.address WHERE library_branch NATURAL JOIN book NATURAL J
问题:我想在 javascript 中混合两种颜色,并得到结果颜色。SO 上有很多类似的问题,但是我没有发现任何实际工作正常的问题。我知道混合两种不同颜色的油漆(颜料)和灯光会产生非常不同的结果(ht
在我的应用程序中,我有一个用于输入 url 的编辑文本,然后编辑文本具有: android:imeOptions="actionDone" 因此,当有人按下键盘的回车键时,应用程序会连接并且键盘会消失
问题:我想在 javascript 中混合两种颜色,并得到结果颜色。SO 上有很多类似的问题,但是我没有发现任何实际工作正常的问题。我知道混合两种不同颜色的油漆(颜料)和灯光会产生非常不同的结果(ht
C-x C-b 显示缓冲区列表。首先是自然顺序,最近使用的缓冲区在顶部,隐藏的缓冲区在底部。 在那里,我现在可以按名称、大小、模式和文件对缓冲区进行排序。但是一旦我点击了这样的选项,我就无法回到原来的
我最近看到了一种叫做 supernova 的编程语言他们在网页上说: The Supernova Programming language is a modern scripting language
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我一直在使用带有附加类型级奇偶校验信息的自然数。 succ 已以最直接的方式成功实现: succ :: Natural p -> Natural (Opp p) succ = Succ 但是,我仍然在
我正在阅读有关 SQL 连接的 NATURAL 速记形式的内容,我发现了一些陷阱: 它只是自动获取所有相同命名的列对(使用 USING 指定显式列列表) 如果添加了一些新列,那么连接输出也可能会“意外
我对 lambda 演算中的自然数有如下定义,这是我的主要目标。 -- Apply a function n times on x apply = \f -> \n -> \x -> foldr ($
我正在尝试按标题对数据库中的项目进行排序。商品有 iphone 5、iphone 6/6S、iphone 11、.. 当我用代码对项目进行排序时 SELECT * FROM ".FPD_PRODUCT
我需要为我的 Web 应用程序对较大的数组(1000-2000 个键)进行复杂的排序。我在 Safari 12.0/FF 66.0 中可以正常使用它,但 chrome 74.0 似乎完全可以做其他事情
我有三个这样描述的表: +----------+ +-----------------+ +----------+ | products | | products_stores |
我创建了一个函数“ADD”来修改变量的值: function ADD(xs, n) { var nom_variable = xs; var XS = eval(xs); no
The Third Commandment的 The Little Schemer状态: When building a list, describe the first typical elemen
我刚看到this我想知道实现这样的自然排序的最佳方法是什么? 通常在 1,4,11,12 列表中,字符串排序(用于列表项)返回 1,11,12,4。如何实现自然排序? 最佳答案 元素也可以是 [ 's
我编写了一些采用异构列表并为其编制索引的代码。 {-# Language GADTs, FunctionalDependencies, MultiParamTypeClasses, KindSigna
所以这个想法是计算机代理将被编程为两层,有意识的和无意识的。 无意识部分本质上是一组输入和输出设备,我通常认为它们是传感器(键盘、温度等,你的想象力的极限)和输出方法(屏幕和扬声器,特别是在家用 PC
我是一名优秀的程序员,十分优秀!