I'm writing a script to import some model objects into the database my django application uses. In the past I've solved this by running ./manage.py shell
and then import myscript
. I'm sure there's a better way. I'd like to be able to call a script from anywhere on my HD using python scriptname.py
, and in the first few lines of that script it would do whatever imports / other operations necessary so that it can access model objects and behave as though it was run using manage.py shell
.
我正在编写一个脚本,将一些模型对象导入到我的django应用程序使用的数据库中。过去我用跑步来解决这个问题。然后导入myscript。我肯定有更好的办法。我希望能够使用python scriptname从HD上的任何地方调用脚本。py,在该脚本的前几行中,它将执行任何必要的导入/其他操作,以便访问模型对象并表现为使用manage运行。py壳。
What do I need to add to my script to achieve this?
为了实现这一点,我需要在脚本中添加什么?
EDIT:
编辑:
Based on @Melug's answer, with addition of dynamically setting Python path to address the 'anywhere on my HD' part of the question:
基于@Melug的回答,加上动态设置Python路径来解决“我的HD上的任何地方”部分的问题:
import sys
sys.path.append('c:\\my_projec_src_folder')
from myproject import settings
from django.core.management import setup_environ
setup_environ(settings)
10 个解决方案
#1
21
You need to setup django environment first:
您需要首先设置django环境:
from your_project import settings
from django.core.management import setup_environ
setup_environ(settings)
At last import your models, everything goes just like django.
最后导入你的模型,一切都像django一样。
#2
104
Since Django 1.4 you should avoid using setup_environ(settings)
(post by Melug) because it is deprecated. Use the following instead and you will be able to access your model
由于Django 1.4不应该使用setup_environmental(设置)(由Melug发布),因为它已经被废弃了。使用以下代码,您将能够访问您的模型
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")
# your imports, e.g. Django models
from your_project_name.models import Location
# From now onwards start your script..
Here is an example to access and modify your model:
下面是一个访问和修改您的模型的例子:
if __name__ == '__main__':
# e.g. add a new location
l = Location()
l.name = 'Berlin'
l.save()
# this is an example to access your model
locations = Location.objects.all()
print locations
# e.g. delete the location
berlin = Location.objects.filter(name='Berlin')
print berlin
berlin.delete()
Example model:
示例模型:
class Location(models.Model):
name = models.CharField(max_length=100)
#3
70
To get models loaded too, I had to combine this with this answer, otherwise I get django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
为了让模型也被加载,我必须把这个和这个答案结合起来,否则我就会得到django.core.exception。收藏还没有准备好:模型还没有装好
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
import django
django.setup()
As an extra, I add this to the __init__.py
of my django projects, it will automatically discover the app name so it is copy/paste-able:
作为一个额外的,我把这个添加到__init__。我的django项目中的py,它会自动发现应用程序名,所以可以复制/粘贴:
import os
def setup():
module = os.path.split(os.path.dirname(__file__))[-1]
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(module))
import django
django.setup()
Then I can just do:
然后我可以做:
from <app> import setup
setup()
#4
29
I think the best way is to create your custom management command(s). Then you can call manage.py <yourcommand>
from anywhere.
我认为最好的方法是创建自定义管理命令。然后你可以打电话给manage。py < yourcommand >从任何地方。
#5
14
For Django version 1.9 or later you can use this:
对于Django 1.9或更高版本,您可以使用以下内容:
import sys
import os
import django
sys.path.append('your_project_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
django.setup()
from yourapp.models import your_model
so you can use object as same django object:
所以你可以使用对象作为django对象:
from myapp.models. import Locations
all_locations = Locations.object.all()
first_location = Locations.object.get(id=1)
print first_location.name()
first_location.save()
#6
4
FOR DJANGO 1.11
DJANGO 1.11
Upper solutions did not work, but gave me an error:
上面的解不起作用,但给了我一个错误:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
django.core.exceptions。外贸英语:应用程序还没有加载。
For me solution from here worked out:
对我来说,从这里解出:
import os
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()
#7
2
Here is the answer for Django versions > 1.4:
以下是Django版本> 1.4的答案:
from django.core.management import settings
from myproject.myproject import settings as project_settings
if not settings.configured:
settings.configure(default_settings=project_settings)
#8
1
Since at least Django 1.11, your main app includes a wsgi module that does the neccessary setup on import. Assuming myproject/myproject
is where your settings.py is, in your script just import:
至少从Django 1.11开始,您的主应用程序包括一个wsgi模块,它在导入时执行必要的设置。假设myproject/myproject是您的设置。py在您的脚本中只是导入:
from myproject.wsgi import application
#9
0
If you get:
如果你得到:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Try:
试一试:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()
#10
0
Try:
试一试:
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
操作系统。环境(“DJANGO_SETTINGS_MODULE”)= " mysite.settings "
if os.environ.setdefault
doesn't work. (Windows 10, python3.6.4, django 2.0.3)
如果os.environ。setdefault行不通。(Windows 10, python3.6.4, django 2.0.3)
#1
21
You need to setup django environment first:
您需要首先设置django环境:
from your_project import settings
from django.core.management import setup_environ
setup_environ(settings)
At last import your models, everything goes just like django.
最后导入你的模型,一切都像django一样。
#2
104
Since Django 1.4 you should avoid using setup_environ(settings)
(post by Melug) because it is deprecated. Use the following instead and you will be able to access your model
由于Django 1.4不应该使用setup_environmental(设置)(由Melug发布),因为它已经被废弃了。使用以下代码,您将能够访问您的模型
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")
# your imports, e.g. Django models
from your_project_name.models import Location
# From now onwards start your script..
Here is an example to access and modify your model:
下面是一个访问和修改您的模型的例子:
if __name__ == '__main__':
# e.g. add a new location
l = Location()
l.name = 'Berlin'
l.save()
# this is an example to access your model
locations = Location.objects.all()
print locations
# e.g. delete the location
berlin = Location.objects.filter(name='Berlin')
print berlin
berlin.delete()
Example model:
示例模型:
class Location(models.Model):
name = models.CharField(max_length=100)
#3
70
To get models loaded too, I had to combine this with this answer, otherwise I get django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
为了让模型也被加载,我必须把这个和这个答案结合起来,否则我就会得到django.core.exception。收藏还没有准备好:模型还没有装好
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
import django
django.setup()
As an extra, I add this to the __init__.py
of my django projects, it will automatically discover the app name so it is copy/paste-able:
作为一个额外的,我把这个添加到__init__。我的django项目中的py,它会自动发现应用程序名,所以可以复制/粘贴:
import os
def setup():
module = os.path.split(os.path.dirname(__file__))[-1]
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(module))
import django
django.setup()
Then I can just do:
然后我可以做:
from <app> import setup
setup()
#4
29
I think the best way is to create your custom management command(s). Then you can call manage.py <yourcommand>
from anywhere.
我认为最好的方法是创建自定义管理命令。然后你可以打电话给manage。py < yourcommand >从任何地方。
#5
14
For Django version 1.9 or later you can use this:
对于Django 1.9或更高版本,您可以使用以下内容:
import sys
import os
import django
sys.path.append('your_project_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
django.setup()
from yourapp.models import your_model
so you can use object as same django object:
所以你可以使用对象作为django对象:
from myapp.models. import Locations
all_locations = Locations.object.all()
first_location = Locations.object.get(id=1)
print first_location.name()
first_location.save()
#6
4
FOR DJANGO 1.11
DJANGO 1.11
Upper solutions did not work, but gave me an error:
上面的解不起作用,但给了我一个错误:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
django.core.exceptions。外贸英语:应用程序还没有加载。
For me solution from here worked out:
对我来说,从这里解出:
import os
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()
#7
2
Here is the answer for Django versions > 1.4:
以下是Django版本> 1.4的答案:
from django.core.management import settings
from myproject.myproject import settings as project_settings
if not settings.configured:
settings.configure(default_settings=project_settings)
#8
1
Since at least Django 1.11, your main app includes a wsgi module that does the neccessary setup on import. Assuming myproject/myproject
is where your settings.py is, in your script just import:
至少从Django 1.11开始,您的主应用程序包括一个wsgi模块,它在导入时执行必要的设置。假设myproject/myproject是您的设置。py在您的脚本中只是导入:
from myproject.wsgi import application
#9
0
If you get:
如果你得到:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Try:
试一试:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()
#10
0
Try:
试一试:
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
操作系统。环境(“DJANGO_SETTINGS_MODULE”)= " mysite.settings "
if os.environ.setdefault
doesn't work. (Windows 10, python3.6.4, django 2.0.3)
如果os.environ。setdefault行不通。(Windows 10, python3.6.4, django 2.0.3)