定义名称时的NameError

时间:2014-10-15 11:52:31

标签: python django

使用python解释器时,我尝试在类中使用导入的类,并获取NameError。但是,在课外使用它时,我没有错误。代码在运行它的文件中运行正常,但有人可以解释这种行为吗?

    In [27]: class DataSheet(factory.DjangoModelFactory):
                    FACTORY_FOR = A.B.C
                    FACTORY_DJANGO_GET_OR_CREATE = ('title',)
                    <other_fields>
      ....:     
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    ~/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
    ----> 1 class DataSheet(factory.DjangoModelFactory):
          2         FACTORY_FOR = A.B.C
          3         FACTORY_DJANGO_GET_OR_CREATE = ('title',)
          4 ...         

    ~/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in DataSheet()
          1 class DataSheet(factory.DjangoModelFactory):
    ----> 2         FACTORY_FOR = A.B.C
          3         # Equivalent to ``FACTORY_FOR = myapp.models.User``
          4         FACTORY_DJANGO_GET_OR_CREATE = ('title',)
          5         

    NameError: name 'A' is not defined

    In [30]: A.D()
    Out[30]: 
   [ <products.Folder instance at 0x32594d0>,
     <products.Folder instance at 0x3259560>,
     <products.Folder instance at 0x30d25f0>]

是的,以下评论很合适 - 我使用的是Django 1.4.11

0 个答案:

没有答案