Python导入问题与Django管理命令 [英] Python import problem with Django management commands

查看:82
本文介绍了Python导入问题与Django管理命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论什么原因,当我刚开始使用Python和Django时,我在models.py文件的顶部写了一些这样的import语句:

For whatever reason, when I was new to Python and Django, I wrote some import statements like this at the top of a models.py file:

from django.contrib import auth

我会像这样使用:

class MyModel(models.Model):
    user = models.ForeignKey(auth.models.User)
    # ...

这工作正常。很久以后,我写了一个自定义管理命令,它会这样做:

This worked fine. A long time later, I wrote a custom management command, and it would do this:

from myapp.models import MyModel

当我运行自定义命令( python manage.py my_command )这将导致Python抱怨模块 auth 没有属性模型在行上声明<$为了解决这个问题,我将models.py更改为更常见的:

When I ran my custom command (python manage.py my_command) this would result in Python complaining that the module auth had no attribute models on the line declaring the ForeignKey in models.py.

/ p>

To work around this problem, I changed my models.py to the more usual:

from django.contrib.auth.models import User

class MyModel(models.Model):
    user = models.ForeignKey(User)
    # ...

有人可以向我解释我失踪了什么?运行管理命令时,环境有什么不同吗?还是我一直在做错事?谢谢!

Can someone explain to me what I am missing? Is there something different in the environment when you run a management command? Or was I just doing it wrong the whole time? Thanks!

编辑:关于圆形导入的dmitko,以下是我的models.py文件中使用的导入。我正在显示原始导入的 auth 注释掉,以及唯一具有auth用户模型外键的模型:

Edit: Following dmitko's hunch about circular imports, here are the imports used in my models.py file. I'm showing the original import of auth commented out, along with the only model that has a foreign key to the auth user model:

import datetime  
from django.db import models 
# from django.contrib import auth
from django.contrib.auth.models import User 

class UserLastVisit(models.Model):
    # user = models.ForeignKey(auth.models.User, unique=True)
    #                          ^^^^^^^^^^^^^^^^
    # after adding mgmt command, error occurred here; change to the line below
    user = models.ForeignKey(User, unique=True)
    last_visit = models.DateTimeField(db_index=True)

这里是导出问题的管理命令的导入:

And here are the imports of the management command that uncovered the problem:

import datetime   
from django.core.management.base import NoArgsCommand 
from core.models import UserLastVisit, AnonLastVisit, Statistic

是否设置了循环导入类型的情况?

Was this setting up a circular import type situation?

推荐答案

导入模块 xyz ,那么稍后导入 xy 的人会看到一个 z xy 命名空间。

If some random module ever imports module x.y.z, then a later person who imports just x.y will see a z in the x.y namespace.

发生这种情况的原因是 import xyz 其实是三个import语句在一个。它的工作原理如下:

The reason this happens is that import x.y.z is actually three import statements in one. It works something like this:

x = __internal_import('x')
x.y = __internal_import('x/y')
x.y.z = __internal_import('x/y/z')

下次有人 __ internal_import('x / y'),他们将获得相同的对象,因为python足够聪明,不能导入同一个对象两次。该对象已经分配给 z 模块的 z 成员。

Next time someone does __internal_import('x/y'), they'll get the same object, because python is smart enough not to import the same one twice. That object already has its z member assigned to the z module.

在你的完整的应用程序,可能你有一个模块, import django.contrib.auth.models 。但是您的最小独立程序没有导入该模块,所以该名称从未被分配。

In your full app, probably you had a module that did import django.contrib.auth.models. But your minimal standalone program didn't import that module, so the name was never assigned.

(注意:没有这样的东西,如 __ internal_import ,这只是一个例证,真正的功能有一些其他名称,你必须查找。)

(Note: there's no such thing as __internal_import. It's just an illustration. The real function has some other name that you would have to look up.)

这篇关于Python导入问题与Django管理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆