簡體   English   中英

如何使用 djongo 添加、檢索和更新未在模型中實現的字段?

[英]How to add, retrieve and update fields which are not implemented in models using djongo?

我在 django 項目中使用 mongoDB 作為我的主數據庫。 我正在使用 djongo 引擎來訪問和操作 mongoDB。 為了測試我寫了一個簡單的學生 model

class StudentModel(models.Model):
    name = model.CharField(max_length=250)
    age = model.IntegerField()

並使用此 model 插入數據

from app.models import StudentModel
StudentModel(name="John" age=10).save()

因為 mongoDB 是無模式數據庫,我想嘗試添加未在 StudentModel 中實現的字段,所以我嘗試插入一個帶有額外 email 地址字段的新學生。

StudentModel(name="Steve" age=9, email="steve@email.com").save()

但它給出了這種類型的錯誤

TypeError:StudentModel() 得到了一個意外的關鍵字參數“電子郵件”

這是我在settings.py

DATABASES = {
    'default': {
           'ENGINE': 'djongo',
           'NAME': 'testdb',
           'ENFORCE_SCHEMA': 'False',
       }
}

我們如何使用 djongo 添加、檢索和更新未在模型中實現的字段?

您可以使用 raw_query 作為選項。

You can add email field to your model as null=True, then if you add email when you are creating new student it will save with email otherwhise email section will be null.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM