I'm trying to figure out this fairly simple (but weird) problem. I need ModelB to have a Foreign Key reference to ModelA (with a CharField PK), like so:
class ModelA(models.Model):
name = models.CharField(primary_key=True)
tags = TaggableManager()
class ModelB(models.Model):
model_a = models.ForeignKey(ModelA)
My code seems to understand this in runtime and I can create ModelA/ModelB objects no problem.
However, when I try to manually add/delete Model A objects in django admin, I get some kind of Value Exception.
Exception Value: invalid literal for int() with base 10:
I know it has something to do with trying to interpret a key CharField as an int() id, but why does my djangowebsite/admin not know how to deal with these relationships?
UPDATE: This is the full stack trace:
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'
UPDATE:
I have a strong feeling it has something to do with my Taggable Manager utility that I'm trying to use. I suspect that it defaults to thinking that my ModelA has a int() pk. Still investigating.
My mistake. It seems its because it doesn't like the fact that I'm using the name as the primary key. I changed it so that the name now has a
unique=True
rule and that fixed the issue.