Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re evaluate django query after changes done to database

I got this long queryset statement on a view

    contributions = user_profile.contributions_chosen.all()\
    .filter(payed=False).filter(belongs_to=concert)\
    .filter(contribution_def__left__gt=0)\
    .filter(contribution_def__type_of='ticket')

That i use in my template

context['contributions'] = contributions

And later on that view i make changes(add or remove a record) to the table contributions_chosen and if i want my context['contributions'] updated i need to requery the database with the same lenghty query.

contributions = user_profile.contributions_chosen.all()\
.filter(payed=False).filter(belongs_to=concert)\
.filter(contribution_def__left__gt=0)\
.filter(contribution_def__type_of='ticket')

And then again update my context

context['contributions'] = contributions

So i was wondering if theres any way i can avoid repeating my self, to reevaluate the contributions so it actually reflects the real data on the database. Ideally i would modify the queryset contributions and its values would be updated, and at the same time the database would reflect this changes, but i don't know how to do this.

UPDATE: This is what i do between the two context['contributions'] = contributions

I add a new contribution object to the contributions_chosen(this is a m2m relation),

contribution = Contribution.objects.create(kwarg=something,kwarg2=somethingelse)
user_profile.contributions_chosen.add(contribution) 
contribution.save()
user_profile.save()

And in some cases i delete a contribution object contribution = user_profile.contributions_chosen.get(id=1) user_profile.contributions_chosen.get(id=request.POST['con contribution.delete()

As you can see i'm modifying the table contributions_chosen so i have to reissue the query and update the context. What am i doing wrong?

UPDATE After seeing your comments about evaluating, i realize i do eval the queryset i do len(contributions) between context['contribution'] and that seems to be problem. I'll just move it after the database operations and thats it, thanks guy.

like image 223
Guillermo Siliceo Trueba Avatar asked May 03 '12 15:05

Guillermo Siliceo Trueba


1 Answers

update Seems you have not evaluated the queryset contributions, thus there is no need to worry about updating it because it still has not fetched data from DB.

Can you post code between two context['contributions'] = contributions lines? Normally before you evaluate the queryset contributions (for example by iterating over it or calling its __len__()), it does not contain anything reading from DB, hence you don't have to update its content.

To re-evaluate a queryset, you could

# make a clone
contribution._clone()
# or any op that makes clone, for example
contribution.filter()

# or clear its cache
contribution._result_cache = None

# you could even directly add new item to contribution._result_cache, 
# but its could cause unexpected behavior w/o carefulness
like image 54
okm Avatar answered Oct 19 '22 20:10

okm