Adding Two Models In Django

50 views
Skip to first unread message

Ridwan Adeyemo

unread,
Jun 15, 2021, 2:34:37 PM6/15/21
to Django users
How do I add two models together in the database, two different DecimalField. 
i.e math_score + eng_score = total_score

patel dhruvish

unread,
Jun 15, 2021, 3:09:33 PM6/15/21
to django...@googlegroups.com
Use foreign key in 1 model for initiat another model...

On Wed, Jun 16, 2021, 00:04 Ridwan Adeyemo <adesolar...@gmail.com> wrote:
How do I add two models together in the database, two different DecimalField. 
i.e math_score + eng_score = total_score

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5eaa4bb6-f411-4952-8a24-09c5a71f60c2n%40googlegroups.com.

Ridwan Adeyemo

unread,
Jun 15, 2021, 3:16:42 PM6/15/21
to django...@googlegroups.com
Thanks for the response. Kindly explain more or provide image description.
Best regards

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/qsaj_ELIfgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANMvxF24E23qK5zXihVcZJs7MBi6tRoaNXL1U%2Buz%2BExBUDNw-Q%40mail.gmail.com.

Sebastian Jung

unread,
Jun 15, 2021, 4:52:20 PM6/15/21
to django...@googlegroups.com
Hello,

You create all modelfields inkl. Total sum and after user submit Form with 2 fields in save() Method you sum both fields together ans write this in total field...

Regards

Ridwan Adeyemo <adesolar...@gmail.com> schrieb am Di., 15. Juni 2021, 20:34:
How do I add two models together in the database, two different DecimalField. 
i.e math_score + eng_score = total_score

--

Ridwan Adeyemo

unread,
Jun 16, 2021, 2:22:25 AM6/16/21
to django...@googlegroups.com
Please kindly describe with images.

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/qsaj_ELIfgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9mxKHBMMSDOsqu0isFL4og_9fQYTKNjvmSqTUtEtY_9NZA%40mail.gmail.com.

Ayush Bisht

unread,
Jun 16, 2021, 12:22:03 PM6/16/21
to Django users
I think you are trying to sum up the value of two model fields and store them in some other field. 


so for that u can proceed from base. 

simple you just have to update that field in save method

for example : 
 class Student(models.Model):
       maths_score = models.DecimalField()
       english_score = models.DecimalFIeld()
       Total_score   = models.DecimalField(default = 0)

       def save(self, *args, **kwargs):
          self.Total_score  = self.maths_score  + self.english_score
          super(Student, self).save(*args, **kwargs)



or you can proceed with clean() method also. 

Ridwan Adeyemo

unread,
Jun 16, 2021, 2:02:51 PM6/16/21
to django...@googlegroups.com
Thanks man.👍

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/qsaj_ELIfgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

Ridwan Adeyemo

unread,
Jun 16, 2021, 5:37:34 PM6/16/21
to django...@googlegroups.com
Good day,
Please how can this be achieved?
Best regards.
IMG_20210616_205040_332.jpg

Ayush Bisht

unread,
Jun 17, 2021, 2:05:20 PM6/17/21
to Django users

class Year(models.Model):
    year = models.IntegerField()


class Month(models.Model):
    MONTH_CHOICE = (
        (1 , "Jan"),
        (2, 'Feb'),
        (3, 'March'),
        (4, 'April'),
        (5, 'May'),
        (6, 'June'),
        (7, 'July'),
        (8, 'Aug'),
        (9, 'Sept'),
        (10, 'Oct'),
        (11, 'Nov'),
        (12, 'Dec')

    )
    month = models.PositiveSmallIntegerField(choices = MONTH_CHOICE)
    year = models.ForeignKey(Year, on_delete= models.SET_NULL)


class Student(models.Model):
    name = models.CharField(max_length=266)
    admission_month = models.ForeignKey(Month, on_delete=models.SET_NULL)


you can proceed with this approach also... 
Reply all
Reply to author
Forward
0 new messages