i build an application, React Native as Frontend and Django as Backend, i use axios to take data from user then send it to backend.
the user has to choose one of these type [ balance, savaing, income, loans, expenses]
i want the value of [ balance] increasing and decreasing depend of other types the savaing, income increasing the value of balance. and loans, expenses decreasing the value of balance .
i am not sure if i have to do the mathematical on the front-end while axios data or on the model on the backend.
the transactions model
from datetime import date
from tkinter import CASCADE
from django.db import models
from authentication.models import User
# Create your models here.
class Transaction(models.Model):
TYPE_OPTIONS = [
('BALANCE', 'BALANCE'),
('SAVING', 'SAVING'),
('LOANS', 'LOANS'),
('INCOME', 'INCOME'),
('EXPENSES', 'EXPENSES')
]
type = models.CharField(choices=TYPE_OPTIONS, max_length=255)
amount = models.CharField(max_length=255)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.CharField(max_length=255)
date = models.CharField(max_length=255)
class FinancialDeateails(models.Model):
balance = models.IntegerField(max_length=255)
savaing = models.IntegerField(max_length=255)
income = models.IntegerField(max_length=255)
loans = models.IntegerField(max_length=255)
expenses = models.IntegerField(max_length=255)
owner = models.OneToOneField(User, on_delete=models.CASCADE)
CodePudding user response:
No sure what you need, may be something like this:
class Kind(models.Model):
label = models.CharField(max_length=255)
value = models.CharField(max_length=255)
TYPE_OPTIONS = [
('BALANCE', 'BALANCE'),
('SAVING', 'SAVING'),
('LOANS', 'LOANS'),
('INCOME', 'INCOME'),
('EXPENSES', 'EXPENSES')
]
@classmethod
def initial(cls):
if not cls.objects.exists():
cls.objects.bulk_create([cls(label=k, value=v) for k, v in cls.TYPE_OPTIONS])
def __str__(self):
return self.label
class Transaction(models.Model):
type = models.ForeignKey(Kind, on_delete=models.CASCADE)
amount = models.CharField(max_length=255)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.CharField(max_length=255)
date = models.CharField(max_length=255)
CodePudding user response:
Do you want to increase or decrease the value of balance from FinancialDeateails when the user will change the value of type in Transaction?
If I get it correctly then you have to make a one-to-one relationship with FinancialDeateails and FinancialDeateails.
Then you can override the save method and increase or decrease the value as per the Transaction type.