Reputation: 1518
I need to update Django queryset bookings_to_save
in such a way:
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:
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?
Upvotes: 1
Views: 61
Reputation: 3364
According to https://docs.djangoproject.com/en/1.7/ref/models/queries/#supported-operations-with-f that does not seem to be supported yet. 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).
One could also imagine a new kind of string expression (S-expressions; sorry Lisp readers):
bookings_to_save.update(
status=Booking.STATUS_CHOICES.approved,
title=S('ST{}{}', F('id'), F('created')))
)
Upvotes: 1