Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的两个相同类型的django对象行为不同?_Python_Django - Fatal编程技术网

Python 为什么我的两个相同类型的django对象行为不同?

Python 为什么我的两个相同类型的django对象行为不同?,python,django,Python,Django,救命啊 我用django中的不同技术创建了两个对象 >>> from django.contrib.contenttypes.models import ContentType >>> from myproject.models import Building 方法A 方法B 我的问题是,为什么同一类型的两个对象行为不同 GathSQL查询仅在调用content\u query[0]时执行,因此,如果content\u类出现问题,查询将在该点失败。至少我注意

救命啊

我用django中的不同技术创建了两个对象

>>> from django.contrib.contenttypes.models import ContentType
>>> from myproject.models import Building
方法A

方法B

我的问题是,为什么同一类型的两个对象行为不同


Gath

SQL查询仅在调用content\u query[0]时执行,因此,如果content\u类出现问题,查询将在该点失败。至少我注意到您忘记了第一行中的对象:

content_type = ContentType.objects.get(app_label='myproject', model='Building')
编辑:当%标记被错误地解释时,我发现'str'对象没有属性'items'错误。这为我解决了这个问题:

s = "%build%"
content_query = content_class.objects.raw("Select * from pms_building where namelike %s", [s])

我猜是两种不同的init方法。使用dirbld和dircontent_query查看它们定义了什么。编辑无关,在我评论时修复=Psorry typo,遗漏了对象编辑了问题,问题仍然存在remains@gath这两种方法对我都有效。您使用的是什么Django版本和DB?@gath我在使用带%-chars的LIKE时出错,请检查我的编辑。
content_type = ContentType.objects.get(app_label='myproject', model='Building')
s = "%build%"
content_query = content_class.objects.raw("Select * from pms_building where namelike %s", [s])