Re-evaluate the django request after making changes to the database

I got this long instruction for request in 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')

What I use in my template

context['contributions'] = contributions

And later in this view, I make changes (adding or removing an entry) to the contrib_chosen table, and if I want to update my context ["installments"], I need to query the database with the same length 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 refresh my context again

context['contributions'] = contributions

So, I wondered if I could in any case avoid repeating myself, overestimate the contribution so that it really reflects the real data in the database. Ideally, I would change the tabs of the request, and its values ​​would be updated, and at the same time, the database would reflect these changes, but I do not know how to do this.

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

_chosen ( m2m),

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

  contrib = user_profile.contributions_chosen.get(id = 1)   user_profile.contributions_chosen.get(ID = request.POST [ '   contribution.delete()

, contrib_chosen, . ?

UPDATE , , eval , () [ "" ], . , , .

+5
2

, contributions, , .

context['contributions'] = contributions? , contributions (, __len__()), , .

,

# 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
+3

, , dict filter dict

query_args = dict(
    payed=False,
    belongs_to=concert,
    contribution_def__left__gt=0,
    contribution_def__type_of='ticket',
)

contributions = user_profile.contributions_chosen.filter(**query_args)

, . args, query_args Python dict, : )

+2

All Articles