Когда я пытаюсь напечатать (или использовать в возвращаемой строке) одно из полей, я получаю следующее ValueError:
недопустимый литерал для int () с основанием 10: 'topic_id'
Вот мой класс модели:
from django.db import models
from django.conf import settings
from languageTests.signals import topic_viewed_signal
from django.contrib.contenttypes.models import ContentType
User = settings.AUTH_USER_MODEL
Topic = settings.TOPIC_MODEL
class TopicProgress(models.Model):
user = models.ForeignKey(User, on_delete='CASCADE') # user instance instance.id
topic = models.ForeignKey(Topic, on_delete='CASCADE')
finished = models.BooleanField(default=False)
started = models.BooleanField(default=False)
class Meta:
unique_together = ('topic', 'user',)
def __str__(self):
print("topic=<{}>".format(self.topic))
return "{}-{}".format(self.user,self.topic)
Также это класс topi c:
class Topic(models.Model):
LEVELS = (
('b', 'Beginner'),
('i', 'Intermediate'),
('e', 'Expert'),
)
level = models.CharField(
max_length=1,
choices=LEVELS,
blank=True,
default='b',
help_text='Tests level',
)
title = models.CharField(max_length=200, help_text='Enter a Test Name (e.g. Test #1)')
objects = TopicManager()
def return_level_str(self):
dict_levels=dict(self.LEVELS)
return dict_levels[self.level]
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('topic', args=[self.id])
Я попытался отладить и выяснил этот «id» отображается в виде строки в C: \ Users \ A \ Anaconda3 \ lib \ site-packages \ django \ db \ models \ query.py в get
args
(<Q: (AND: ('id', 'topic_id'))>,)
kwargs
{}
self
<QuerySet [<Topic: sel>, <Topic: Past Perfect>]>
Я также пытался напечатать другие поля «готово», «запущено», «пользователь», и все они работали отлично. Может кто-нибудь пролить свет на это для меня?
вот полный след:
traceback:
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
163. rel_obj = self.field.get_cached_value(instance)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\fields\mixins.py" in get_cached_value
13. return instance._state.fields_cache[cache_name]
During handling of the above exception ('topic'), another exception occurred:
File "C:\Users\A\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\A\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
156. response = self.process_exception_by_middleware(e, request)
File "C:\Users\A\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response
154. response = response.render()
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\response.py" in render
106. self.content = self.rendered_content
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\response.py" in rendered_content
83. content = template.render(context, self._request)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\contrib\admin\templatetags\base.py" in render
33. return super().render(context)
File "C:\Users\A\Anaconda3\lib\site-packages\django\template\library.py" in render
214. _dict = self.func(*resolved_args, **resolved_kwargs)
File "C:\Users\A\Anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py" in result_list
341. 'results': list(results(cl))}
File "C:\Users\A\Anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py" in results
318. yield ResultList(None, items_for_result(cl, res, None))
File "C:\Users\A\Anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py" in __init__
309. super().__init__(*items)
File "C:\Users\A\Anaconda3\lib\site-packages\django\contrib\admin\templatetags\admin_list.py" in items_for_result
251. if str(result_repr) == '':
File "C:\Users\A\Desktop\The-place\EnForFun\EnForFun\progress\models.py" in __str__
21. print("topic=<{}>".format(self.topic))
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
177. rel_obj = self.get_object(instance)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\fields\related_descriptors.py" in get_object
144. return qs.get(self.field.get_reverse_related_filter(instance))
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\query.py" in get
390. clone = self.filter(*args, **kwargs)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\query.py" in filter
844. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
862. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\sql\query.py" in add_q
1263. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\sql\query.py" in _add_q
1281. current_negated, allow_joins, split_subq)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\sql\query.py" in _add_q
1287. split_subq=split_subq,
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\sql\query.py" in build_filter
1225. condition = self.build_lookup(lookups, col, value)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\sql\query.py" in build_lookup
1096. lookup = lookup_class(lhs, rhs)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\lookups.py" in __init__
20. self.rhs = self.get_prep_lookup()
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
70. return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\A\Anaconda3\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
965. return int(value)
Exception Type: ValueError at /admin/progress/progressprofile/
Exception Value: invalid literal for int() with base 10: 'topic_id'