我试图找出这个相当简单(但很奇怪)的问题。 我需要ModelB对ModelA(带有CharField PK)的外键引用,如下所示:
class ModelA(models.Model):
name = models.CharField(primary_key=True)
tags = TaggableManager()
class ModelB(models.Model):
model_a = models.ForeignKey(ModelA)
我的代码似乎在运行时理解这一点,我可以创建ModelA / ModelB对象没问题。
但是,当我尝试在django admin中手动添加/删除Model A对象时,我会遇到某种Value Exception。
Exception Value: invalid literal for int() with base 10:
我知道它与尝试将关键字CharField解释为int()id有关,但为什么我的djangowebsite / admin不知道如何处理这些关系?
更新: 这是完整的堆栈跟踪:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:5000/admin/src/ratedmodel/add/
Django Version: 1.7.7
Python Version: 3.4.3
Installed Applications:
('src',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'taggit')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\venv\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\venv\lib\site-packages\django\contrib\admin\options.py" in wrapper
583. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\venv\lib\site-packages\django\utils\decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "C:\venv\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "C:\venv\lib\site-packages\django\contrib\admin\sites.py" in inner
206. return view(request, *args, **kwargs)
File "C:\venv\lib\site-packages\django\contrib\admin\options.py" in add_view
1453. return self.changeform_view(request, None, form_url, extra_context)
File "C:\venv\lib\site-packages\django\utils\decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "C:\venv\lib\site-packages\django\utils\decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "C:\venv\lib\site-packages\django\utils\decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\venv\lib\site-packages\django\db\transaction.py" in inner
394. return func(*args, **kwargs)
File "C:\venv\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1405. self.save_related(request, form, formsets, not add)
File "C:\venv\lib\site-packages\django\contrib\admin\options.py" in save_related
1067. form.save_m2m()
File "C:\venv\lib\site-packages\django\forms\models.py" in save_m2m
100. f.save_form_data(instance, cleaned_data[f.name])
File "C:\venv\lib\site-packages\taggit\managers.py" in save_form_data
373. getattr(instance, self.name).set(*value)
File "C:\venv\lib\site-packages\taggit\utils.py" in inner
136. return func(self, *args, **kwargs)
File "C:\venv\lib\site-packages\taggit\managers.py" in set
184. self.clear()
File "C:\venv\lib\site-packages\taggit\utils.py" in inner
136. return func(self, *args, **kwargs)
File "C:\venv\lib\site-packages\taggit\managers.py" in clear
194. self.through.objects.filter(**self._lookup_kwargs()).delete()
File "C:\venv\lib\site-packages\django\db\models\manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\venv\lib\site-packages\django\db\models\query.py" in filter
691. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\venv\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
709. clone.query.add_q(Q(*args, **kwargs))
File "C:\venv\lib\site-packages\django\db\models\sql\query.py" in add_q
1331. clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\venv\lib\site-packages\django\db\models\sql\query.py" in _add_q
1358. current_negated=current_negated, connector=connector)
File "C:\venv\lib\site-packages\django\db\models\sql\query.py" in build_filter
1230. condition = self.build_lookup(lookups, col, value)
File "C:\venv\lib\site-packages\django\db\models\sql\query.py" in build_lookup
1138. return final_lookup(lhs, rhs)
File "C:\venv\lib\site-packages\django\db\models\lookups.py" in __init__
82. self.rhs = self.get_prep_lookup()
File "C:\venv\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
85. return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "C:\venv\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
1607. return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "C:\venv\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
646. return self.get_prep_value(value)
File "C:\venv\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
1601. return int(value)
Exception Type: ValueError at /admin/src/ratedmodel/add/
Exception Value: invalid literal for int() with base 10: 'ModelAName'
更新:
我强烈认为它与我尝试使用的Taggable Manager实用程序有关。我怀疑它默认认为我的ModelA有一个int()pk。还在调查。
答案 0 :(得分:0)
我的错误。这似乎是因为它不喜欢我使用名称作为主键的事实。我对其进行了更改,以便该名称现在具有unique=True
规则并修复了该问题。