1

更新された問題は解決しました。ここにいくつかの設計上の問題があります。

ディレクトリは次のようになります。

/view
  |-__init__.py
  |-quiz.py
  |-test.py
  |-user.py

そして問題は、で、からquiz.pyをインポートすることです。で、からをインポートします。classtesttest.pyclassquiz

更新:変更しましimportたが、まだありますAttributeError

次のようなコード:

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

エラーは

Exception Type: AttributeError Exception
Value: 'module' object has no attribute 'Quiz'

最初は再帰的な問題かもしれないと思ったのですが、import再帰的なインポートを避けるために関数に移動できただけですが、ここには関数がなく、importクラスに移動しようとすると機能しません。

これらの定義を別のファイルに保持する方法はありますか?

4

3 に答える 3

3

これは、古典的な循環インポートの状況です。「from test import Test」を使用する代わりに、単に「import test」を実行してから、test.Test で Test にアクセスできます。詳細については、このスタックオーバーフローの質問を参照してください。

于 2012-03-30T10:24:55.160 に答える
1

QuizCheckoutを別のモジュールに移動します。(QuizCheckoutはクラス定義レベルでテストを参照し、テストは問題の根本であるクイズを参照します)

于 2012-03-30T10:56:47.110 に答える
0

hymloth の記述が正しければ (私は試していません)、次のようにして "Test" という名前を再度使用できるはずです。

import test
Test = test.Test
于 2012-03-30T10:28:33.723 に答える