MongoEngine: ReferenceField accepts only DBRef or documents when defining document_type as str

In one MongoEngine model, I use the link field when I use

schedule = ReferenceField('Schedule',required=True) 

and try pasting a document

 #my_schedule being a 'Schedule' object that has been created and saved successfully record.schedule = my_schedule record.save() 

I get

 ValidationError: ValidationError (Calling:None) (A ReferenceField only accepts DBRef or documents: ['schedule']) 

However, if I changed the field definition to

 schedule = ReferenceField(path.to.Schedule,required=True) 

(IE directly refers to the schedule model)

The document can be saved successfully. How can I avoid this error?

Full smoothing of the schedule model

 class Schedule(Document): uid = StringField(required=True) start = DateTimeField(required=True) end = DateTimeField(required=True) days = ListField(required=True) toc = StringField(required=False) meta = { 'indexes':['uid'] } 

And to call

 class Calling(Document): """ Calling Point """ schedule = ReferenceField('Schedule',required=True) tiploc = StringField(required=True) calling = ListField(StringField(required=True)) arrive = IntField(required=False) depart = IntField(required=False) meta = { 'indexes':[('schedule','calling','tiploc','depart'),('schedule','tiploc')] } 

Python 2.7, MongoEngine 0.8.2, PyMongo 2.5.2

Update

In accordance with the request; _document_registry output

 {'Calling': <class 'models.calling.Calling'>, 'Schedule': <class 'models.schedule.Schedule'>, 'Station': <class 'models.station.Station'>, 'Stop': <class 'models.stop.Stop'>, 'Train': <class 'models.train.Train'>, 'Update': <class 'models.update.Update'>} 

Folder layout;

 β”œβ”€β”€ app β”‚  β”œβ”€β”€ controllers β”‚  β”œβ”€β”€ models β”‚  └── views └── tasks 

app / models manages all my models, this particular error occurs in /update.py tasks (imports data file in Mongo, runs as cron-job). app / is added to the system path when the application loads, if that matters.

in application / models I have one file per model, so app / models / schedule.py contains Schedule, etc.

And yes, you are right that I solved the problem by accessing the object directly, however this creates a cyclical dependency problem between call.py and schedule.py, while I could move the Calling and Schedule models to the same file, which I do not want, because I am a stubborn programmer and, as a rule, a single-model for each file, despite the fact that it does not have a real difference =)

Update 2

Adding

 print type(my_schedule), schd, type(Schedule) 

gives me

 <class 'app.models.schedule.Schedule'> Schedule object <class 'mongoengine.base.metaclasses.TopLevelDocumentMetaclass'> 
+6
source share
1 answer

The definitions look great, I feel the import error is masked by a validation error message.

When using Calling , you have to import the Schedule somewhere in your code so that it exists in the document class repository at that time. Do /update.py tasks include both Calling and Schedule models?

+1
source

Source: https://habr.com/ru/post/946909/


All Articles