Reputation: 11
I'm working on a project where I've to add the first and second semester scores together. I summed up the first semester results in my models and also for the second semester also. But I've difficult summing the two models i.e the first and second. Here's my code for better understanding.
class First(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
midscore = models.FloatField()
finalscore = models.FloatField()
def__str__(self):
return self.user
def get_total_first_score(self):
return self.midscore + self.finalscore
class Second(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
midscore = models.FloatField()
finalscore = models.FloatField()
def__str__(self):
return self.user
def get_total_second_score(self):
return self.midscore + self.finalscore
Now, how can I add the ger_total_score
for the two semester. I tried something like this, but it's not giving me any results, since it's not under any model.
def get_total_scores:
get_total_first_score + get_total_second_score
Upvotes: 0
Views: 89
Reputation: 4194
I suggest you to combine they fields into one model for better use. For example:
class Score(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
STATUS_CHOICES = (
('first_score', 'First Score'),
('second_score', 'Second Score')
)
status = models.CharField(max_length=20, default='first_score',
choices=STATUS_CHOICES)
midscore = models.FloatField()
finalscore = models.FloatField()
@property
def total_scores(self):
return self.midscore + self.finalscore
def __str__(self):
return '%s' % self.user
And in ORM, you can create dual object of Score
with different status
, for example:
>>> first_score = Score.objects.create(user=user, status='first_score',
midscore=95.2, finalscore=83.0)
>>>
>>>
>>> second_score = Score.objects.create(user=user, status='second_score',
midscore=72.1, finalscore=78.5)
>>>
>>> total_scores = first_score.total_scores + second_score.total_scores
328.79999999999995
Store all fields into one model. For example:
class Score(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
status = models.CharField(max_length=20, default='first_score',
choices=STATUS_CHOICES)
first_midscore = models.FloatField(blank=True, null=True)
first_finalscore = models.FloatField(blank=True, null=True)
second_midscore = models.FloatField(blank=True, null=True)
second_finalscore = models.FloatField(blank=True, null=True)
@property
def first_total_scores(self):
midscore = self.first_midscore or 0
finalscore = self.first_finalscore or 0
return midscore + finalscore
@property
def second_total_scores(self):
midscore = self.second_midscore or 0
finalscore = self.second_finalscore or 0
return midscore + finalscore
@property
def total_scores(self):
return self.first_total_scores + self.second_total_scores
def __str__(self):
return '%s' % self.user
So, in your ORM you just need to create one object of Score
.
>>> # step 1, create the `first` scores.
>>> Score.objects.create(
user=user,
first_midscore=95.2,
first_finalscore=83.0
)
>>>
>>> # step 2, use created object
>>> Score.objects.get_or_create(
user=user,
second_midscore=72.1,
second_finalscore=78.5
)
>>>
>>> # step 3, get the total of both scores.
>>> score = Score.objects.get(user=user)
>>> score.first_total_scores
178.2
>>> score.second_total_scores
150.6
>>> score.total_scores
328.79999999999995
But, If you realy want to use as your own method, you can use this;
def get_total_scores(first_object, second_object):
first_score = first_object.get_total_first_score()
second_score = second_object.get_total_second_score()
return first_score + second_score
In your ORM;
>>> first = First.objects.create(user=user, midscore=95.2, finalscore=83.0)
>>>
>>> second = Second.objects.create(user=user, midscore=72.1, finalscore=78.5)
>>>
>>> get_total_scores(first, second)
328.79999999999995
But, I think that case just same with;
>>> first.get_total_first_score() + second.get_total_second_score()
328.79999999999995
Upvotes: 1