Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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_Postgresql - Fatal编程技术网

Python 可以在Django中查询查询对象吗?

Python 可以在Django中查询查询对象吗?,python,django,postgresql,Python,Django,Postgresql,我试图从django查询我的postgres数据库,我使用的查询是 s = Booking.objects.all().filter(modified_at__range=[last_run, current_time], coupon_code__in=l) 现在,我正在脚本中以某种方式更改我的这个对象,而不是将其保存到数据库中。我想知道的是,现在可以查询这个对象吗 比如,我把变量改为 s.modified_at = '2016-02-22' 是否仍可以按以下方式查询此对象: s.obje

我试图从django查询我的postgres数据库,我使用的查询是

s = Booking.objects.all().filter(modified_at__range=[last_run, current_time], coupon_code__in=l)
现在,我正在脚本中以某种方式更改我的这个对象,而不是将其保存到数据库中。我想知道的是,现在可以查询这个对象吗

比如,我把变量改为

s.modified_at = '2016-02-22'
是否仍可以按以下方式查询此对象:

s.objects.all()

或者类似的东西?

QueryManager是Django的数据库接口(ORM)。根据定义,这意味着您只能查询存储在数据库中的数据

因此,简而言之:“不”。不能对未保存的数据进行查询

想想你为什么要问这个问题,尤其是看使用“modified_at”的示例:为什么你不想保存数据?
(顺便说一句,您可能希望对“修改位置”字段使用
auto\u now=True

您可以执行以下操作:

bookings = Booking.objects.all().filter(modified_at__range=[last_run, current_time], coupon_code__in=l)
for booking in bookings:
   booking.modified_at = 'some value'
   booking.save() # now booking object will have the updated value

对不起,不清楚你在问什么
s
是一个查询集,它没有
modified\u at
属性,也没有
objects
管理器。总之,您使用的过滤器返回一个列表,s.modified\u将抛出一个错误。过滤器返回一个列表,我的意思是我更改了该列表中的值(即s)。更改对象有什么用,不保存它,然后再次查询?