[英]Django queryset update based on existent fields
I need to update Django queryset bookings_to_save
in such a way: 我需要更新Django的查询集
bookings_to_save
以这样一种方式:
for booking in bookings_to_save:
booking.status = Booking.STATUS_CHOICES.approved
booking.title = 'ST{}{}'.format(booking.pk, booking.created.strftime('%Y%m%d'))
booking.save()
I tried something like this: 我尝试过这样的事情:
bookings_to_save.update(
status=Booking.STATUS_CHOICES.approved,
title='ST{}{}'.format(F('id'), F('created'))
)
In SQL I would do like this: 在SQL中,我会这样:
update bookings_booking bb set title='ST' || bb.id || to_char(bb.created, 'YYYYMMDD') where << some condition >>;
Is there any way how can I accomplish this task using django tools? 有什么方法可以使用Django工具完成此任务吗?
According to https://docs.djangoproject.com/en/1.7/ref/models/queries/#supported-operations-with-f that does not seem to be supported yet. 根据https://docs.djangoproject.com/zh-CN/1.7/ref/models/queries/#supported-operations-with-f ,似乎尚未受支持。 Ideally it would look like:
理想的情况是:
bookings_to_save.update(
status=Booking.STATUS_CHOICES.approved,
title='ST' + F('id') + F('created'))
)
because the operations supported are operators on F
(the F
s need to be part of the outer expression to form an F-expression). 因为支持的操作是
F
上的运算符( F
必须是外部表达式的一部分才能形成F表达式)。
One could also imagine a new kind of string expression (S-expressions; sorry Lisp readers): 也可以想象一种新型的字符串表达式(S-表达式;对不起Lisp读者):
bookings_to_save.update(
status=Booking.STATUS_CHOICES.approved,
title=S('ST{}{}', F('id'), F('created')))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.