我正在编写一个脚本,将一些模型对象导入我的django应用程序使用的数据库中.在过去,我通过运行./manage.py shell然后解决了这个问题import myscript.我相信有更好的方法.我希望能够从我的HD上的任何地方调用脚本python scriptname.py,并且在该脚本的前几行中它将执行任何必要的导入/其他操作,以便它可以访问模型对象并且表现得好像它已经运行使用manage.py shell.
我需要添加到脚本中才能实现此目的?
编辑:
基于@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)
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码来填充我的数据库:
import os
def populate():
python_cat = add_cat('Python')
add_page(cat=python_cat,
title="Official Python Tutorial",
url="http://docs.python.org/2/tutorial/")
add_page(cat=python_cat,
title="How to Think like a Computer Scientist",
url="http://www.greenteapress.com/thinkpython/")
add_page(cat=python_cat,
title="Learn Python in 10 minutes",
url="http://www.korokithakis.net/tutorials/python/")
django_cat = add_cat(name="Django")
add_page(cat=django_cat,
title="Official Django Tutorial",
url="http://djangoproject.com/en/1.5/intro/tutorial01/")
add_page(cat=django_cat,
title="Django Rocks",
url="http://www.djangorocks.com/")
add_page(cat=django_cat,
title="How to Tango with Django",
url="htttp://www.tangowithdjango.com/")
frame_cat = add_cat(name="Other Frameworks")
add_page(cat=frame_cat,
title="Bottle",
url="http://bottlepy.org/docs/dev/")
add_page(cat=frame_cat,
title="Flask",
url="http://flask.pocoo.org")
# Print out what we have added to the user.
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print "- {0} - …Run Code Online (Sandbox Code Playgroud) 我正在尝试将项目从Django 1.6升级到1.7.到目前为止,我已经创建了一个具有所有相同安装的新virtualenv,并将Django版本升级到新版本.我需要从南升级,但有错误这样做,所以我想我最初只是尝试runserver,我得到以下错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Name/Dev/tps/products/models.py", line 127, in <module>
watson.register(Product.objects.exclude(productimage=None))
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/Name/.virtualenvs/test17/lib/python2.7/site-packages/django/db/models/query.py", line 698, in …Run Code Online (Sandbox Code Playgroud) 当我今天想在服务器上部署django项目时,我一直坚持这个过程.当我python manage.py runserver在服务器上运行时,终端显示我:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
commands = get_commands()
File "/usr/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps …Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/utils/translation/trans_real.py", line 164, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
Run Code Online (Sandbox Code Playgroud)
我有一个项目,它不是一个django应用程序,而是一个芹菜应用程序.因此,我还没有创建一个wsgi.py或models.py或通过创建典型的文件django-admin时,一个项目或应用程序已启动.
我只想使用djcelery能够使用djcelery.schedules.DatabaseScheduler此处指定的类似方法创建周期性任务在运行时添加,修改,删除celery.schedules和此处如何动态添加/删除周期性任务到Celery(celerybeat)
这里给出的问题的解决方案(AppRegistryNotReady,使用uWSGI部署时的转换错误)要求我对vassal.ini文件进行更改.我的实现中没有vassal.ini文件.
我将简要介绍一下我的项目 -
proj
apps.py
tasks.py
celeryconfig.py
runproj.py
Run Code Online (Sandbox Code Playgroud)
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celeryconfig')
myapp = Celery('myapp')
myapp.config_from_object('celeryconfig')
if __name__ == '__main__' …Run Code Online (Sandbox Code Playgroud) 如何在解释器级别删除 Django 数据库中的一行。我正在对数据库进行故障排除,我希望能够定位 Django sqlite3 数据库中的特定行。
目前我可以在数据库中创建条目如下:
>> Item.objects.create(text='Item A')
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法在数据库中查看:
>> for p in Item.objects.raw('SELECT * FROM appName_model'):
... print(p.text)
...
Run Code Online (Sandbox Code Playgroud)
我正确地获得了数据库内容(他们的文本字段)。
通过索引、text_field 或某些其他属性定位特定行时遇到问题。我直接在 Python 解释器上进行故障排除。
django ×6
python ×4
celery ×1
celerybeat ×1
database ×1
delete-row ×1
django-1.7 ×1
django-1.8 ×1
djcelery ×1
sqlite ×1