无法使用 djongo "Array Model Field" 将数据添加到 MongoDB
Can't to add a data into MongoDB using djongo "Array Model Field"
我正在尝试使用 "Array Model Field"(djongo) 添加数据,如图 Djongo Documentation(Array Model Field) 或
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
进入admin.py我在管理面板中添加了模型注册
from django.contrib import admin
from .models import Entry
admin.site.register(Entry)
当我尝试通过 http://localhost:8000/admin/ 添加数据时,我遇到了 MigrationError...
我的错误在哪里?我有什么不明白的?
我是个笨蛋。对不起。更新模型后我没有'makemigration'。
所以这就是我为让它发挥作用所做的工作:
1. 更新模型后,我做了 'python manage.py makemigrations' 并打开了电源。
您应该在所有模型上使用 models.ObjectIdField() 以避免调用 django 迁移。
示例:
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
中查看更多内容
我正在尝试使用 "Array Model Field"(djongo) 添加数据,如图 Djongo Documentation(Array Model Field) 或
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
进入admin.py我在管理面板中添加了模型注册
from django.contrib import admin
from .models import Entry
admin.site.register(Entry)
当我尝试通过 http://localhost:8000/admin/ 添加数据时,我遇到了 MigrationError...
我的错误在哪里?我有什么不明白的?
我是个笨蛋。对不起。更新模型后我没有'makemigration'。
所以这就是我为让它发挥作用所做的工作: 1. 更新模型后,我做了 'python manage.py makemigrations' 并打开了电源。
您应该在所有模型上使用 models.ObjectIdField() 以避免调用 django 迁移。
示例:
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
中查看更多内容