【问题标题】:Import model Django in MondoDB shell whith Djongo使用 Django 在 MongoDB shell 中导入模型 Django
【发布时间】:2020-07-09 16:28:11
【问题描述】:

我想在 MondoDB shell 中的 django 中导入模型。 我在 Django 中的模型类

class User(models.Model):
    name = models.CharField(max_length=20)

我在 mongo shell 中使用它

from app.models import User 

错误:

[js] SyntaxError: missing ; before statement @(shell):1:6

【问题讨论】:

  • 据我所知,您无法导入 Django 模型。毕竟这些都是 Python 对象,而 mongo db 只是数据库上的一个外壳。

标签: python-3.x django mongodb djongo


【解决方案1】:

简短回答:您不能在 MongoDB shell 中导入 Django 模型。

mongo 数据库对 Django 一无所知。模型是通过向数据库写入查询来构建的。对于 ORM 查询,它也会进行 Mongo 查询。

模型类是 python 类。因此,Mongo 不了解这些类,但它甚至不知道其中涉及到 Web 应用程序等。它只是维护一个文档集合并在这些文档上运行查询。

您可以打开Django shell [Django-doc] 但是:

./manage.py <b>shell</b>

这将加载 Django 应用程序并显示一个 Python shell,您可以在其中编写 Django ORM 命令。

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 2018-02-23
    • 1970-01-01
    • 2016-08-20
    • 2016-12-21
    • 2018-12-27
    • 1970-01-01
    • 2014-10-07
    • 2011-10-07
    相关资源
    最近更新 更多