Если вам нужно решить проблему с простым SQL, вы можете использовать подзапрос.
select count(*) from (select * from foo) as x;
Well, I think I have to answer to this. The problem is that Django caches its models in a singleton (singleton like structure) called AppCache. Basically, to reload Django models you need to first reload and re-import all the model modules stored in the AppCache. Then you need to wipe out the AppCache. Here's the code for it:
import os
from django.db.models.loading import AppCache
cache = AppCache()
curdir = os.getcwd()
for app in cache.get_apps():
f = app.__file__
if f.startswith(curdir) and f.endswith('.pyc'):
os.remove(f)
__import__(app.__name__)
reload(app)
from django.utils.datastructures import SortedDict
cache.app_store = SortedDict()
cache.app_models = SortedDict()
cache.app_errors = {}
cache.handled = {}
cache.loaded = False
I've put all of this in a separate file called reloadmodels.py in the root directory of my Django site. Using IPython I can reload everything by running:
%run ~/mysite/reloadmodels.py
ipython console выполняет глубокую перезагрузку с каждым выражением reload ()
; и, конечно же, добавляет много других полезных вещей.
Предположим, ваш проект настроен таким образом
первая загрузка
from bookstore.shelf.models import Books
последующие перезагрузки
import bookstore;reload(bookstore.shelf.models);from bookstore.shelf.models import Books
Насколько мне известно, ни одно из вышеперечисленных решений не работало само по себе, также этот поток сам по себе не очень помог, но после объединения подходов мне удалось перезагрузить свои модели. в shell_plus :
models.pyc
Очистить кеш модели Django (например, здесь ):
из django.db.models.loading import AppCache
cache = AppCache ()
из django.utils.datastructures import SortedDict
cache.app_store = SortedDict ()
cache.app_models = SortedDict ()
cache.app_errors = {}
cache.handled = {}
cache.loaded = Ложь
Обновите модель, например здесь
перезагрузите (project.app.models)
из project.app.models импортировать MyModel