Ashish Acharya
Ashish Acharya

Reputation: 3399

Getting variable from queryset

I have a ModelAdmin class, to which I have added a custom action called add_50_credits as follows:

class TutorAdmin(admin.ModelAdmin):
    ...
    actions = ['add_50_credits']

    def add_50_credits(self, request, queryset):
        queryset.update(account_balance+=50)

What I am trying to do is add 50 to the account_balance of all users in the queryset. However, this code gives a syntax error. How do I get the variable account_balance for each object in the queryset and add 50 to it?

Upvotes: 0

Views: 82

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

You use F objects.

from django.db.models import F
queryset.update(account_balance=F('account_balance') + 50)

Upvotes: 2

Related Questions