Updated Problem solved, I have some design problem here.
The directory looks like that:
/view
|-__init__.py
|-quiz.py
|-test.py
|-user.py
And the problem is that in quiz.py
, I import a class
from test
. and in test.py
, I import a class
from quiz
.
Updated: I changed import
but there is still a AttributeError
The code as following:
quiz.py
#ignore some imports here
import test
from user import User
class Quiz(Document):
creator = ReferenceField(User, reverse_delete_rule=CASCADE)
info = GenericEmbeddedDocumentField("QuizInfo")
description = StringField(max_length=100)
attachment = GenericEmbeddedDocumentField("QuizAttach")
correctanswer = GenericEmbeddedDocumentField("QuizAnswer")
wronganswer = GenericEmbeddedDocumentField("QuizAnswer")
manualdifficulty= FloatField(min_value=0, max_value=1)
autodifficulty = FloatField(min_value=0, max_value=1)
checkout = GenericEmbeddedDocumentField("QuizCheckcout")
tag = ListField(StringField(max_length=20))
#ignore some codes here
class QuizCheckout(EmbeddedDocument):
time = DateTimeField()
type = IntField()
description = StringField()
test = ReferenceField(test.Test, reverse_delete_rule=CASCADE)
test.py
import quiz
class Test(Document):
createdate = DateTimeField() #Create datetime
description = StringField() #decription of this test
takennumber = IntField() #the number of students who may take this test
quiz = GenericEmbeddedDocumentField('TestQuiz')
class TestQuiz(EmbeddedDocument):
quiz = ListField(ReferenceField(quiz.Quiz, reverse_delete_rule=CASCADE))
#Reference to Quiz, if Quiz is deleted, this reference will be deleted too.
correct = IntField()
#how many students got this right
and the error is
Exception Type: AttributeError Exception
Value: 'module' object has no attribute 'Quiz'
At first I thought that maybe a recursive problem, but I only find that I could move import
into functions to avoid recursive import, but there is no functions here, and I try to move import
into class, it don't work.
Is there any way to keep these definition in separate file?