B
B
Boldy2014-11-23 20:01:16
Django
Boldy, 2014-11-23 20:01:16

How to use getattr/setattr in .update() request in django?

There is a design:

rewarded_parents_fields = ('first_parent_rewarded',
                           'second_parent_rewarded',
                           'third_parent_rewarded',
                           'fourth_parent_rewarded', )

def pay_to_parent(self, line, five_per_period):
        ...
        if line == 1:
            TreeNode.objects.filter(id=self.id).update(first_parent_rewarded=True)
        elif line == 2:
            TreeNode.objects.filter(id=self.id).update(second_parent_rewarded=True)
        elif line == 3:
            TreeNode.objects.filter(id=self.id).update(third_parent_rewarded=True)
        elif line == 4:
            TreeNode.objects.filter(id=self.id).update(fourth_parent_rewarded=True)

How can I shorten pay_to_parent to one line to make it pretty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Scherbakov, 2014-11-23
@Boldy

Let's say like this.

TreeNode.objects.filter(id=self.id).update(**{rewarded_parents_fields[line - 1]: True})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question