我试图访问部分模型自我属性,但是在迁移后出现了这个错误。
在我运行makemigrations
和migrate
之前,模型运行得很好。
在阅读了值错误页面后,我注意到值错误是由于模型试图在属性函数下访问它自己的外键(choosen_room_1
)。
Traceback:
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/theblackcat/website/backend/views.py" in dashboard
54. return render_to_response('form/dashboard.html', RequestContext(request, locals()))
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
39. content = loader.render_to_string(template_name, context, using=using)
File "/Users/theblackcatwebsite/env/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
210. return self._render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _render
202. return self.nodelist.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _render
202. return self.nodelist.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
329. return nodelist.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
217. nodelist.append(node.render(context))
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render
89. output = self.filter_expression.resolve(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in resolve
648. obj = self.var.resolve(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in resolve
789. value = self._resolve_lookup(context)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
827. current = getattr(current, bit)
File "/Users/theblackcat/website_koref/backend/models.py" in total_price
226. price,_ = self.getTotalPricing()
File "/Users/theblackcat/website/backend/models.py" in getTotalPricing
212. if self.choosen_room_1 is not None:
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
610. qs = qs.filter(**params)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/query.py" in filter
679. return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
697. clone.query.add_q(Q(*args, **kwargs))
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1310. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1338. allow_joins=allow_joins, split_subq=split_subq,
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1209. condition = self.build_lookup(lookups, col, value)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_lookup
1102. return final_lookup(lhs, rhs)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
105. self.rhs = self.get_prep_lookup()
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
143. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
727. return self.get_prep_value(value)
File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
985. return int(value)
Exception Type: ValueError at /en/backend/
Exception Value: invalid literal for int() with base 10: 'choosen_room_1_id'
models.py
def getTotalPricing(self):
total_price = 0
amount = dict()
if self.choosen_room_1 is not None:
choosen_room_1
属性的定义如下:
models.py
choosen_room_1 = models.ForeignKey(ChoosenRoom, default=None, related_name="room_type_1", null=True)
到目前为止,我尝试过的一些事情是:
删除旧的迁移文件
检查表中是否存在使用sqlite终端的id字段,并且它确实存在
强制choosen_room_1 to_field为id
或choosenroom_id
,同样的错误仍然存在
使用Django管理界面我确信选择室不是空的,并且能够用新的外键项重新分配它。
使用manage.py shell访问choosen_room_1会给我带来同样的错误。
在阅读了一些stackoverflow问题之后,我的猜测是某种方式,外键正在期待具有整数类型的唯一id键,但是在查询过程中出现了问题? As mention here
顺便说一下,我使用Django 1.8.18
在Python 2.7下运行这个项目更新 刷新数据库后,花了一些时间用类似的数据重新填充数据库,它现在就像一个魅力。