嵌入式字段:必须是模型的实例:<class 'django.db.models.base.Model'>

Embedded Field: must be instance of Model: <class 'django.db.models.base.Model'>

我尝试使用对象管理器为 Entry 模型(从 djongo 模型扩展)做一个虚拟播种器,但在保存时出现错误。

错误:必须是模型的实例:`

Python 脚本

<The complete python script used to produce the issue.>

from djongo import models
from django.core.management.base import BaseCommand, CommandError

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

    class Meta:
        abstract = True

class Entry(models.Model):
    _id = models.ObjectIdField()
    blog = models.EmbeddedField(
        model_container=Blog
    )

    headline = models.CharField(max_length=255)
    objects = models.DjongoManager()


def build_dummy_entry():
    e = Entry.objects.create(
    headline='h1',
    blog={
        'name': 'b1',
        'tagline': 't1'
    })

    g = Entry.objects.get(headline='h1')
    assert e == g

    e = Entry()
    e.blog = {
        'name': 'b2',
        'tagline': 't2'
    }
    e.headline = 'h2'
    e.save()


class Command(BaseCommand):
    help='Create a preset dummy entry'

    def handle(self, *args, **options):
        try:
            build_dummy_entry()
            self.stdout.write(self.style.SUCCESS(f'Successfully created dummy blog'))
        except Exception as e:
            raise CommandError(f'{e}')

追溯
CommandError: Value: {'name': 'b1', 'tagline': 't1'} must be instance of Model: <class 'django.db.models.base.Model'>

---编辑解决方案---

我使用的是 1.3.1。 我检查了版本 1.3.2 和 1.3.3,看起来这些版本 包含对实例化错误的修复。

如错误所述,您应该使用模型实例,但您使用的是字典。

def build_dummy_entry():
    e = Entry.objects.create(
        headline='h1',
        blog=Blog(**{'name': 'b2', 'tagline': 't2'}),
    )

    ...