> I have a straightforward form that simply reduces an item count by 1, for a
> selected product. (The form essentially "ships" a product, so need to
> decrease remaining term_length by 1).
>
> I have the following code:
>
> cd = form.cleaned_data
>
> q = Subscription.objects.filter(product_key__exact =
> cd['product'])
> q = q.filter(status__iexact='Active')
> q = q.filter(term_length__gt=0)
>
> # Decrement our terms remaining by 1.
> rec_count = q.update(term_length=F('term_length') - 1)
>
> return render_to_response('subs/shipped.html',{ 'q':
> q, 'rec_count': rec_count })
>
> What happens is that the results I want to display on the "shipped" page
> are still being filtered according to the criteria I used to select the
> rows for updating. Since I have a >= 0 filter, I do not see those rows that
> were reduced from 1 to 0. I can't use {{ q|length }} to get number of
> records updated, for example.
>
> How would I go about returning, for example, a list of the freshly updated
> records from BEFORE they were updated. I can't search after the fact, since
> status and term_length would be Inactive and 0 for a huge number of
> records; there will be nothing distinct about the ones that were last
> changed.
>
> Thanks for the help.
>
> M.
It seems your best bet is to do something like this:
to_be_updated = list(q.select_for_update())
q.filter(pk__in=to_be_updated).update(...)
The above is written in a way trying to guarantee that the
to_be_updated set is exactly the same as the actual set you are going
to update. However, you need to fetch the objects from the database
which can be costly. If you don't want to do that, you could fetch
just the PK list or add another column to the update discriminating
the set (modified_by=someid). It could be useful if you could directly
get the updated PK list from .update(), but that is not supported by
the ORM.
- Anssi
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment