Answer the question
In order to leave comments, you need to log in
Why is the table not being written during save() in django?
I'm making an app for a network marketing company. The subtree level is called "line". That is, those whom I invited - the first line, those who were invited by the second line - the third, and so on.
When 5 newcomers are collected on the 1st line - A is charged for each of them
When 5 newcomers are collected on the 2nd line - B is charged for each of them When
5
newcomers are collected on the 3rd line - C is charged for each of them
D is charged for each of them
Implementation of the payout in the second piece of code. In the first - the model of the participant of the pyramid.
Now the problem is: when I create n 5 participants on 1, 3, or 4 participant lines, he himself is credited not 5A, 5C, 5D money, respectively, but A, C and D. That is, 5 times less. But for the second line, he gets it right. Some kind of magic. I feel that I messed up somewhere with the save () method, but I don’t understand where. 5 CompanyToClientPayment objects are always created as expected, each of them is marked as paid, but in fact only one is paid.
Saving a new member of the "pyramid" and checking if it's time to charge parents money. Don't pay attention to the five_per_period parameter:
def pay_to_parent(self, line, five_per_period):
parent = self.get_parent_of_level(line)
if parent:
from payments.models import CompanyToClientPayment
if line == 1:
payment_to_parent = CompanyToClientPayment(client=parent,
total=prices[0]
use_date=date.today())
payment_to_parent.save()
elif line == 2:
payment_to_parent = CompanyToClientPayment(client=parent,
total=prices[1]
use_date=date.today())
payment_to_parent.save()
elif line == 3:
payment_to_parent = CompanyToClientPayment(client=parent,
total=prices[2],
use_date=date.today())
payment_to_parent.save()
elif line == 4:
payment_to_parent = CompanyToClientPayment(client=parent,
total=prices[3],
use_date=date.today())
payment_to_parent.save()
setattr(self, self.rewarded_parents_fields[line - 1], True)
def save(self, newbie=True, *args, **kwargs):
if newbie:
if self.get_parent_of_level(1):
newbie_brothers = self.get_parent_of_level(1).get_relative_level(level=1, newbies_only=True)
newbie_brothers.append(self)
if len(newbie_brothers) == 5:
for newbie in newbie_brothers:
newbie.pay_to_parent(1, five_per_period=True)
if newbie.get_parent_of_level(2):
newbie.pay_to_parent(2, five_per_period=True)
if self.get_parent_of_level(3):
newbie_brothers = self.get_parent_of_level(3).get_relative_level(level=3, newbies_only=True)
newbie_brothers.append(self)
if len(newbie_brothers) == 5:
for newbie in newbie_brothers:
newbie.pay_to_parent(3, five_per_period=False)
if self.get_parent_of_level(4):
newbie_brothers = self.get_parent_of_level(4).get_relative_level(level=4, newbies_only=True)
newbie_brothers.append(self)
if len(newbie_brothers) == 5:
for newbie in newbie_brothers:
newbie.pay_to_parent(4, five_per_period=False)
super(TreeNode, self).save(*args, **kwargs)
class CompanyToClientPayment(models.Model):
total = models.PositiveIntegerField(verbose_name='Сумма платежа', blank=False, null=False)
use_date = models.DateField(verbose_name='Дата платежа', blank=False)
client = models.ForeignKey(TreeNode, verbose_name='Участник')
is_paid = models.BooleanField(default=False, verbose_name='Выплачено')
def save(self, *args, **kwargs):
if not self.is_paid:
if self.use_date <= date.today():
self.pay()
self.client.save(newbie=False)
super(CompanyToClientPayment, self).save(*args, **kwargs)
def pay(self):
if not self.is_paid:
self.client.capital += self.total
self.is_paid = True
Answer the question
In order to leave comments, you need to log in
In my opinion, you generally complicate things too much, this code does not fit in my head. Let me start by simplifying it. Instead of all branches of ifs in the first function, you can write:
I don't quite understand why your save() takes a newbie parameter . You are overriding a function that is defined in models.Model (or MPTTModel ). Signatures must not contradict.
It seems that you have several copies of the same tree member object, you modify one of them, and the rest remain unchanged.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question