models.py
class ChangingDocument(PrivateCommentMixin,
models.Model):
# Список изменяющих документов.
treaty = models.ForeignKey("treaties.Treaty",
on_delete=models.CASCADE,
blank=False,
null=False,
related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss", )
changing_document = models.ForeignKey("treaties.Treaty",
on_delete=models.CASCADE,
blank=False,
null=False,
related_name="%(app_label)s_%(class)_changing_documents_related",
related_query_name="%(app_label)s_%(class)ss_changing_documents", )
def __str__(self):
return str(self.changing_document)
堆栈跟踪
(venv) michael@michael:/media/michael/750/Programming/treaty_outer/treaty_project$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 416, in execute
django.setup()
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/apps/registry.py", line 116, in populate
app_config.import_models()
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/apps/config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/media/michael/750/Programming/treaty_outer/treaty_project/changing_documents/models.py", line 6, in <module>
class ChangingDocument(PrivateCommentMixin,
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/db/models/base.py", line 194, in __new__
new_class.add_to_class(obj_name, obj)
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/db/models/base.py", line 371, in add_to_class
value.contribute_to_class(cls, name)
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 866, in contribute_to_class
super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
File "/media/michael/750/Programming/treaty_outer/venv/lib/python3.8/site-packages/django/db/models/fields/related.py", line 357, in contribute_to_class
related_name %= {
ValueError: unsupported format character '_' (0x5f) at index 22
问题
我从这里通过例子做了模型:https://docs.djangoproject.com/en/4.2/topics/db/models/#abstract-related-name
你能帮帮我吗?
1条答案
按热度按时间6gpjuf901#
在
related_name='%(app_label)s_**%(class)**_changing_documents_related'
中,您在%(class)
之后忘记了s
,因此格式化程序不知道如何格式化class
字符串(作为字符串s
,或作为reprr
,或其他内容)。因此,您需要在 * 下划线之前添加s
*,因此:简而言之,使用
%(
var_name)
是不够的,你可以使用%(
var_name)
specifier``,其中指定符通常是s
或r
。对于r
,它将调用repr
,因此:将通过
repr
传递'class'
,从而添加额外的引号。然而,在这里这样做没有多大意义。