我正在运行基于查询的其他ID的查询.我遇到的问题是,有时查询将找不到结果.而不是让整个程序崩溃,我如何检查结果是否为None?
这是我的查询:
sub_report_id = DBSession.query(TSubReport.ixSubReport).filter(and_(TSubReport.ixSection==sectionID[0], TSubReport.ixReport== reportID[0])).one()
Run Code Online (Sandbox Code Playgroud)
当代码执行并且没有找到结果时,我得到一个NoResultFound异常
NoResultFound: No row was found for one()
Run Code Online (Sandbox Code Playgroud)
如果没有结果,有没有办法跳过查询?
在SO上找到解决方案(之前找不到) 从sqlalchemy获取第一行
我得到了这个奇怪的错误,我说的很奇怪,因为我改变了一个不相关的表.
我试图查询我的tDevice表,看起来像这样:
class TDevice(Base):
__tablename__ = 'tDevice'
ixDevice = Column(Integer, primary_key=True)
ixDeviceType = Column(Integer, ForeignKey('tDeviceType.ixDeviceType'), nullable=False)
ixSubStation = Column(Integer, ForeignKey('tSubStation.ixSubStation'), nullable=False)
ixModel = Column(Integer, ForeignKey('tModel.ixModel'), nullable=True)
ixParentDevice = Column(Integer, ForeignKey('tDevice.ixDevice'), nullable=True)
sDeviceName = Column(Unicode(255), nullable=False)#added
children = relationship('TDevice',
backref=backref('parent', remote_side=[ixDevice]))
device_type = relationship('TDeviceType',
backref=backref('devices'))
model = relationship('TModel',
backref=backref('devices'))
sub_station = relationship('TSubStation',
backref=backref('devices'))
Run Code Online (Sandbox Code Playgroud)
这就是我查询它的方式:
Device = DBSession.query(TDevice).filter(TDevice.ixDevice == device_id).one()
Run Code Online (Sandbox Code Playgroud)
一旦执行此行,我就会收到错误:
ArgumentError: relationship 'report_type' expects a class or a mapper argument (received: <class 'sqlalchemy.sql.schema.Table'>)
Run Code Online (Sandbox Code Playgroud)
我所做的唯一更改是在my中添加一个report_type关系tReportTable
,现在看起来像这样:
class TReport(Base):
__tablename__ …Run Code Online (Sandbox Code Playgroud) 我有一个表格,我想要获取所有列名称但是在浏览网站后我找不到可行的方法.这就是我的表格:
class myTable(Base):
__tablename__ = 'myTable'
col1 = Column(Integer, primary_key=True)
col2 = Column(Unicode(10))
col3 = Column(Integer)
col4 = Column(Numeric(10, 6))
col5 = Column(Numeric(6,3))
col6 = Column(Numeric(6,3))
child = relationship('tChild',
backref=backref('children'))
Run Code Online (Sandbox Code Playgroud)
我希望能够从for循环中打印所有列名.例如:
"col1", "col2", "col3".... etc
Run Code Online (Sandbox Code Playgroud)
使用常规sql这很容易,但我似乎无法弄清楚如何使用sqlAlchemy表模型
我正在尝试使用cmd创建一个超级用户,但每次我运行命令时我都会:
C:\Users\PCDOM\Desktop\Power\pm_app>python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
312, in execute
django.setup()
File "C:\Python27\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python27\lib\site-packages\django\apps\registry.py", line 85, in popu
late
app_config = AppConfig.create(entry)
File "C:\Python27\lib\site-packages\django\apps\config.py", line 86, in create
module = import_module(entry)
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named compressor
Run Code Online (Sandbox Code Playgroud)
即使我在我的虚拟环境和settings.py中安装了它:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#'bootstrap3',
'promo', …Run Code Online (Sandbox Code Playgroud) 我试图通过C#创建一个Word文档.我希望能够在我的代码中的某个位置插入分页符,但是我不确定如何执行此操作.我主要使用StringBuilder来创建html.这是在Visual Studio 2010中用c#完成的.我看了一些指南,但大多数都使用像"Word变量"这样的代码.所以我不确定"Word"是来自额外下载的库还是什么不是.
django当我通过命令提示符运行我的项目并执行时python manage.py runserver,我的项目运行良好,但现在我正在尝试调试,所以我正在通过 Visual Studio 2013 运行我的项目,它似乎也在工作。我决定更改我的端口号,所以我右键单击了我的 django 项目并选择了属性。在属性选项卡中,我设置Port Number为 8000。当我尝试在 Visual Studio 中运行我的项目时,我不断收到此错误:
ImportError: Could not import settings '8000' (Is it on sys.path? Is there an import error in the settings file?): No module named 8000
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
然后我尝试通过不指定8000为我的端口来恢复为随机端口号,但它仍然给我同样的错误,除了不同的随机端口号。我试图到处寻找以找出原因,但我似乎找不到太多。当我没有指定端口号时,它会给我同样的错误,除了不同的随机端口号。
通读文档我什os.environ.setDefault("DJANGO_SETTINGS_MODULE","pm_app.settings")至尝试更改为
os.environ["DJANGO_SETTINGS_MODULE"] = "pm_app.settings"
Run Code Online (Sandbox Code Playgroud) 我有一个名为"ddlCaseFiles"的DropDownList.然后我有一个导入的脚本,其中包含用于捕获事件的jQuery .change()函数.然后我有3个单选按钮,对dropDownList进行排序.加载默认单选按钮后,事件将触发.但是一旦我选择了另一个单选按钮,那么事件就不再发生了.
这是我的DropDownList:
<asp:DropDownList runat="server" ID="ddlCaseFiles" DataSourceID="dsMyCaseFiles"
DataTextField="Display" DataValueField="FileID" OnPreRender="ddl_PreRender" />
Run Code Online (Sandbox Code Playgroud)
这是我的jQuery .change():
$('#ddlCaseFiles').change(function () {
debugger;
$('#lblNextExhibitNumber').text('');
var temp = $(this).val();
});
Run Code Online (Sandbox Code Playgroud)
以下是单选按钮的功能:
protected void rbByFileName_CheckedChanged(object sender, EventArgs e)
{
ddlCaseFiles.DataSourceID = "dsCaseFilesReverse";
ddlCaseFiles.DataTextField = "Display";
ddlCaseFiles.DataValueField = "FileID";
}
protected void rbByFileID_CheckedChanged(object sender, EventArgs e)
{
ddlCaseFiles.DataSourceID = "dsCaseFiles";
ddlCaseFiles.DataTextField = "Display";
ddlCaseFiles.DataValueField = "FileID";
}
Run Code Online (Sandbox Code Playgroud)
我之前从未使用过.change().也许我错过了一些东西
python ×5
pyramid ×3
sqlalchemy ×3
asp.net ×2
django ×2
c# ×1
javascript ×1
jquery ×1
ms-word ×1