简体   繁体   English

无法使用djongo“数组模型字段”将数据添加到MongoDB中

[英]Can't to add a data into MongoDB using djongo “Array Model Field”

I'm trying to add a data using "Array Model Field"(djongo) as shown Djongo Documentation(Array Model Field) or 我正在尝试使用“数组模型字段”(djongo)添加数据,如Djongo文档(数组模型字段)所示

from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        abstract = True

class MetaData(models.Model):
    pub_date = models.DateField()
    mod_date = models.DateField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    class Meta:
        abstract = True

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    class Meta:
        abstract = True

    def __str__(self):
        return self.name

class Entry(models.Model):
    blog = models.EmbeddedModelField(
        model_container=Blog,
    )
    meta_data = models.EmbeddedModelField(
        model_container=MetaData,
    )

    headline = models.CharField(max_length=255)
    body_text = models.TextField()

    authors = models.ArrayModelField(
        model_container=Author,
    )
    n_comments = models.IntegerField()

    def __str__(self):
        return self.headline

Into admin.py I added for registration of model in admin panel 我添加到admin.py中,用于在管理面板中注册模型

from django.contrib import admin
from .models import Entry

admin.site.register(Entry)

And when I try to add a data via http://localhost:8000/admin/ I have a MigrationError... 当我尝试通过http:// localhost:8000 / admin /添加数据时,出现了MigrationError ...

Where is my mistake? 我的错误在哪里? And what am I not understanding? 我不明白什么?

I'm a stupid. 我很傻 Sry. 对不起 I didn't 'makemigration' after update a model. 更新模型后,我没有“ makemigration”。

And so here's what I did to make it work: 1. After update a model I did 'python manage.py makemigrations' and that power on. 因此,这就是我要做的工作:1.更新模型后,我执行了“ python manage.py makemigrations”并打开了电源。

You should use models.ObjectIdField() on all models to avoid calling django migrations. 您应该在所有模型上使用models.ObjectIdField()以避免调用django迁移。

Example: 例:

class Author(models.Model):
    _id = models.ObjectIdField()
    name = models.CharField(max_length=200)
    email = models.EmailField()

class Meta:
    abstract = True

def __str__(self):
    return self.name

See more in Djongo Docs Djongo文档中查看更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM