基本上是这样的
ValidationError (MatterUser:51dc53babc6163617b38c2fe) (A ReferenceField only accepts DBRef or documents 1.A ReferenceField only accepts DBRef or documents 2.A ReferenceField only accepts DBRef or documents 3.A ReferenceField only accepts DBRef or documents 4.A ReferenceField only accepts DBRef or documents 5.A ReferenceField only accepts DBRef or documents 6.A ReferenceField only accepts DBRef or documents 7.A ReferenceField only accepts DBRef or documents 8.A ReferenceField only accepts DBRef or documents 9.A ReferenceField only accepts DBRef or documents 10.A ReferenceField only accepts DBRef or documents 11.A ReferenceField only accepts DBRef or documents 12.A ReferenceField only accepts DBRef or documents 13.A ReferenceField only accepts DBRef or documents 14.A ReferenceField only accepts DBRef or documents 15.A ReferenceField only accepts DBRef or documents 16.A ReferenceField only accepts DBRef or documents: ['groups'])
我的模型似乎正确(仅显示相关位)
class MatterUser(Document):
groups = ListField(ReferenceField('Grouping'))
homeGroup = ReferenceField('Grouping')
...
class PermissionHolder(Document):
meta = {'allow_inheritance': True}
...
class Grouping(PermissionHolder):
...
视图中的调用类似于
def createGroup(info):
group = Grouping()
group.save()
info['owner'].groups+=group
info['owner'].save() # The offending line
...
newUser = MatterUser(...)
newUser.save()
info = {'owner': newUser,...}
newUser.homeGroup = createGroup(info)
我没有得到的是为什么我会得到这么多验证错误,甚至是问题所在。
编辑:我也意识到给出的错误等于模型中作为ReferenceField的字段数。在这种情况下,分组有16个字段
答案 0 :(得分:1)
当我将其更改为
时info['owner'].groups.append(group)
它执行了预期的行为并且没有错误
答案 1 :(得分:1)
在python中对列表执行+=
要求您添加的项目是可迭代的。然后,每个单独的项目都将附加到列表中。
在MongoEngine Documents和EmbeddedDocuments中是可迭代的,并返回字段名称,例如:
class Participant(EmbeddedDocument):
first_name = StringField()
last_name = StringField()
participant = Participant(first_name="Ross", last_name="Lawley")
您现在可以将文档解压缩为字段名称的变量,例如:
field1_name, field2_name = participant
这就是为什么您收到验证错误+=
正在将每个字段名称添加到groups
列表中。你真正想要做的就是将一个项目附加到你的列表中是正确的,例如:
info['owner'].groups.append(group)