我正在将Django连接到有嵌入式文档的MongoDB。我遵循了https://djongo.readthedocs.io/docs/using-django-with-mongodb-data-fields/上的文档。但是,我收到错误“模块“ django.db.models”没有属性'EmbeddedModelField”
models.py
from django.db import models
from django import forms
from datetime import datetime
class Code(models.Model):
code = models.CharField(max_length=3)
quantity = models.IntegerField()
purchasePrice = models.FloatField(default=1, blank=False, null=False)
datetimeBuy = models.DateTimeField(default=datetime.now, blank=False, null=False)
class Meta:
abstract = True
class Portfolio(models.Model):
username = models.CharField(primary_key=True, max_length=65, default="", blank=False, null=False)
codes = models.EmbeddedModelField(model_container=Code,)
堆栈跟踪
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x043D9ED0>
Traceback (most recent call last):
File "C:\Python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Python\lib\site-packages\django\core\management\commands\runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "C:\Python\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "C:\Python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Python\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Python\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Python\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Administrator\Desktop\WebApps\Django\StockView\backend\src\portfolio\models.py", line 25, in <module>
class Portfolio(models.Model):
File "C:\Users\Administrator\Desktop\WebApps\Django\StockView\backend\src\portfolio\models.py", line 29, in Portfolio
codes = models.EmbeddedModelField(model_container=Code,)
AttributeError: module 'django.db.models' has no attribute 'EmbeddedModelField'
谢谢。
答案 0 :(得分:1)
在djongo 1.3.1之前存在'EmbeddedModelField',之后将其转换为'EmbeddedField'
2。现在,它会给您类似“ django.core.exceptions.AppRegistryNotReady:模型尚未加载”的错误。
因此将djongo从默认的1.3.2降级到1.3.1