Reputation: 2292
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?
Upvotes: 1
Views: 1655
Reputation: 25197
Move QuizCheckout to a separate module. (QuizCheckout references Test at the class definition level, and Test references Quiz, that is the root of the problem)
Upvotes: 1
Reputation: 810
If what hymloth writes is correct (I did not try it out) you should also be able to use the name "Test" again by doing this:
import test
Test = test.Test
Upvotes: 0
Reputation: 7035
This is a classical cyclic import situation. Instead of using "from test import Test", you can simply "import test" and then access Test by test.Test. For more info see this stackoverflow question.
Upvotes: 3