如何解决Django OneToOneField模型错误?

2024-09-30 22:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个模型,需要一对一的关系,一个由很多公式使用的原料。这是我的代码:

from django.db import models
from dashboard.models import Formulas, Feedstock

class FeedstockFormulas(models.Model):
    ratio = models.FloatField()
    feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
    formulas = models.ForeignKey(Formulas, on_delete=models.CASCADE)

这就是我得到的错误:

Traceback (most recent call last):
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 798, in __init__
    to._meta.model_name
AttributeError: module 'dashboard.models.Feedstock' has no attribute '_meta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/config.py", line 211, 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 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/__init__.py", line 1, in <module>
    from .Formulas import Formulas
  File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Formulas.py", line 2, in <module>
    from dashboard.models import FeedstockFormulas
  File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 4, in <module>
    class FeedstockFormulas(models.Model):
  File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 6, in FeedstockFormulas
    feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1049, in __init__
    super().__init__(to, on_delete, to_field=to_field, **kwargs)
  File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 800, in __init__
    assert isinstance(to, str), (
AssertionError: OneToOneField(<module 'dashboard.models.Feedstock' from '/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Feedstock.py'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'

我无法真正解决它,因为ForeignKey的第一个参数已经是一个模型了


Tags: djangoinpyimporthomeinitmodelslib
1条回答
网友
1楼 · 发布于 2024-09-30 22:12:18

看起来这是某个地方循环进口的副产品

试着换成

feedstock = models.OneToOneField("dashboard.Feedstock", on_delete=models.CASCADE, default="0")
formulas = models.ForeignKey("dashboard.Formulas", on_delete=models.CASCADE)

相关问题 更多 >