如何在Django模型中删除ManyToManyField(在Wagtail页面继承者中使用)?

huangapple go评论56阅读模式
英文:

How can I delete ManyToManyField on Django Model (used in Wagtail Page inheritor)?

问题

I have such field:

from wagtail.models import Page

class MyModel(Page):
    ...
    many_to_many_field = models.ManyToManyField(ToClass, blank=False, related_name='+', verbose_name=_("My models"))
    ...

I tried simply to comment it like this:

from wagtail.models import Page

class MyModel(Page):
    ...
    # many_to_many_field = models.ManyToManyField(ToClass, blank=False, related_name='+', verbose_name=_("My models"))

But I've got 'django.core.exceptions.FieldError' while was executing 'python manage.py makemigrations':

Traceback (most recent call last):
  File "D:\projects\myapp\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Python311\Lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "D:\Python311\Lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Python311\Lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Python311\Lib\site-packages\django\core\management\base.py", line 443, in execute
    self.check()
  File "D:\Python311\Lib\site-packages\django\core\management\base.py", line 475, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\wagtail\admin\checks.py", line 70, in get_form_class_check
    if not issubclass(edit_handler.get_form_class(), WagtailAdminPageForm):
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\wagtail\admin\panels\base.py", line 162, in get_form_class
    return get_form_for_model(
           ^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\wagtail\admin\panels\base.py", line 48, in get_form_for_model
    return metaclass(class_name, tuple(bases), form_class_attrs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\wagtail\admin\forms\models.py", line 124, in __new__
    new_class = super(WagtailAdminModelFormMetaclass, cls).__new__(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    new_class = super().__new__(mcs, name, bases, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\modelcluster\forms.py", line 259, in __new__
    new_class = super().__new__(cls, name, bases, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python311\Lib\site-packages\django\forms\models.py", line 327, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (many_to_many_field) specified for MyModel

What is wrong? How can I delete it?

英文:

I have such field:

from wagtail.models import Page

class MyModel(Page):
    ...
    many_to_many_field = models.ManyToManyField(ToClass, blank=False, related_name=&#39;+&#39;, verbose_name=_(&quot;My models&quot;))
    ...

I tried simply to comment it like this:

from wagtail.models import Page

class MyModel(Page):
    ...
    # many_to_many_field = models.ManyToManyField(ToClass, blank=False, related_name=&#39;+&#39;, verbose_name=_(&quot;My models&quot;))

But I've got 'django.core.exceptions.FieldError' while was executing 'python manage.py makemigrations':

Traceback (most recent call last):
  File &quot;D:\projects\myapp\manage.py&quot;, line 10, in &lt;module&gt;
    execute_from_command_line(sys.argv)
  File &quot;D:\Python311\Lib\site-packages\django\core\management\__init__.py&quot;, line 446, in execute_from_command_line
    utility.execute()
  File &quot;D:\Python311\Lib\site-packages\django\core\management\__init__.py&quot;, line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File &quot;D:\Python311\Lib\site-packages\django\core\management\base.py&quot;, line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File &quot;D:\Python311\Lib\site-packages\django\core\management\base.py&quot;, line 443, in execute
    self.check()
  File &quot;D:\Python311\Lib\site-packages\django\core\management\base.py&quot;, line 475, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\django\core\checks\registry.py&quot;, line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\wagtail\admin\checks.py&quot;, line 70, in get_form_class_check
    if not issubclass(edit_handler.get_form_class(), WagtailAdminPageForm):
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\wagtail\admin\panels\base.py&quot;, line 162, in get_form_class
    return get_form_for_model(
           ^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\wagtail\admin\panels\base.py&quot;, line 48, in get_form_for_model
    return metaclass(class_name, tuple(bases), form_class_attrs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\wagtail\admin\forms\models.py&quot;, line 124, in __new__
    new_class = super(WagtailAdminModelFormMetaclass, cls).__new__(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    new_class = super().__new__(mcs, name, bases, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\modelcluster\forms.py&quot;, line 259, in __new__
    new_class = super().__new__(cls, name, bases, attrs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;D:\Python311\Lib\site-packages\django\forms\models.py&quot;, line 327, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (many_to_many_field) specified for MyModel

What is wrong? How can I delete it?

答案1

得分: 0

你的代码中某处仍然引用了已删除的many_to_many_field - 很可能它仍然在content_panels的定义中列出 - 在继续之前,你需要移除这个引用。

英文:

You have a reference to the deleted many_to_many_field somewhere else in your code - most likely, it's still listed in the content_panels definition - and you'll need to remove this reference before you can proceed.

huangapple
  • 本文由 发表于 2023年6月9日 14:24:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定