简体   繁体   中英

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

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

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...

Where is my mistake? And what am I not understanding?

I'm a stupid. Sry. I didn't 'makemigration' after update a model.

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.

You should use models.ObjectIdField() on all models to avoid calling django migrations.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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