我正在使用postgresql後端的django應用程序。我正在使用單獨的python腳本訪問表(通過django模型)進行一些修改,然後啓動修改記錄的Web表單。在我的測試中,我已經進入腳本之外的數據庫並刪除了記錄,當我再次運行腳本時,刪除的記錄仍然存在。爲什麼會發生這種情況,如何確保每次運行腳本時刷新表格?我試着迭代我正在訪問的模型中的對象,並使用object.refresh_from_db()
,但這似乎沒有任何作用。誰能幫忙?通過django訪問模型對象時,從表中刪除的對象仍然存在持續存在
下面是一個代碼片段,其中我試圖訪問現有的記錄:
from django.conf import settings
# from update import update_defaults
# settings.configure(default_settings=update_defaults, DEBUG=True)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mapindex.settings'
django.setup()
# from django.conf import settings
from update.models import Mapindex
for m in Mapindex.objects.all():
m.refresh_from_db()
existing_oid_list.append(m.objectid)
print m.sheetlabel, m.county, m.status
if m.sheetlabel in img_list_shtlbls and m.status == 'Active':
print 'setting', m.sheetlabel, 'status to superseded...'
print 'superseded objectid =', m.objectid
m.status = 'Superseded'
m.publish = 'No'
m.save()
什麼其他信息將是有益的發佈?這是我的settings.py如果是有幫助的:
"""
Django settings for mapindex project.
Generated by 'django-admin startproject' using Django 1.11a1.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'urpz542i6mx*d07dbe&^ko1)u2w+z7n1er=6k913+)3ot89h2d'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'update.apps.UpdateConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_windows_tools',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mapindex.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mapindex.wsgi.application'
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS': {
'options': '-c search_path=django,public'
},
'NAME': 'rowbasedata',
'USER': 'postgres',
'PASSWORD': 'pg*admin',
'HOST': '10.32.2.193',
'PORT': '5432'
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'OPTIONS' : {
'options': '-c search_path=django,sde'
},
'NAME': 'rowbasedata',
'USER': 'postgres',
'PASSWORD': 'pg*admin',
'HOST': '10.32.2.193',
'PORT': '5432'
}
}
# Password validation
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Los_Angeles'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'mapindex', 'static')
STATIC_URL = '/mapindex/static/'
沒有看到你所擁有的一些代碼,很難提供幫助。當你從數據庫中刪除一個對象時,它就消失了,並且不會被新的Django查詢集拾取。也許你正在使用緩存的QS?也許你沒有真正刪除對象?也許腳本中的某些東西正在重新創建它? – Geotob
我可以發佈上面的一些代碼,我自己我看不到問題出現在代碼中。我還注意到,如果我將記錄添加到腳本之外的表中/不使用django,那麼當我運行該腳本時,它不會看到它們。 – kflaw