Jared Joke
Jared Joke

Reputation: 1356

Mongoengine throwing tons of validation errors when saving a model

Basically something like this

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'])

My models seem right though (only showing relevant bits)

class MatterUser(Document):
    groups = ListField(ReferenceField('Grouping'))
    homeGroup = ReferenceField('Grouping')
    ...


class PermissionHolder(Document):
    meta = {'allow_inheritance': True}
    ...

class Grouping(PermissionHolder):
    ...

And the calls in views goes like

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)

What I don't get is why I get so many validation errors or even what the problem is.

Edit: I've also realized that the errors that's given is equal to the number of fields in the model that is the ReferenceField. In this case Grouping has 16 fields

Upvotes: 1

Views: 466

Answers (2)

Ross
Ross

Reputation: 18101

In python doing a += to a list requires the item you are adding to be iterable. Then each individual item will be appended to the list.

In MongoEngine Documents and EmbeddedDocuments are iterable and return the field names eg:

class Participant(EmbeddedDocument):
    first_name = StringField()
    last_name = StringField()

participant = Participant(first_name="Ross", last_name="Lawley")

You can now unpack the document into variables for the field name eg:

field1_name, field2_name = participant

That is why you are getting the validation error += is adding each field name to the groups list. You are correct in what you really want to do is append a single item to your list eg:

info['owner'].groups.append(group)

Upvotes: 1

Jared Joke
Jared Joke

Reputation: 1356

Ugh, finally figured it out. Because of how mongoengine refers to documents and the way python handles += when adding things to a list it basically tried to add every single field to the list.

When I changed it to

info['owner'].groups.append(group)

It performed the intended behavior and was error free

Upvotes: 1

Related Questions