-1

I am new to Django. I want the functionality of Accounts and Transactions. Account can have three types of transactions. Income, Expense and Transfer. All of them have some common fields among them. But also have some additional fields. I want to know can I make such models so that I can access all the transactions of account. And I also want to use them in rest framework. Is it possible to do so?

from django.db import models

class Account(models.Model):
     name = models.CharField(max_length=50)
     balance = models.DecimalField(max_digits=7, decimal_places=1)
     def __str__(self):
         return self.name


class Transaction(models.Model):
    amount = models.DecimalField(max_digits=7, decimal_places=1)
    title = models.CharField(max_length=200)
    date_created = models.DateTimeField(auto_now=True,auto_now_add=False)
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='account')

    def __str__(self):
         return str(self.amount)


class Income(models.Model):
    source = models.CharField(max_length=200)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')


class Expense(models.Model):
    category = models.CharField(max_length=200)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')


class Transfer(models.Model):
    to = models.ForeignKey(Account, on_delete= models.CASCADE, related_name='to')
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')
Umer Maqsood
  • 117
  • 1
  • 11

1 Answers1

0

I have tried to understand your question and I'll answer based on my understanding.

So your question is like you've to create multiple models(Account, Transaction(Income, Expense, Transfer)) and refer those details(transaction of a account) in a stretch.

I probably answer your question, You can create 5 different models as you mentioned.

Models:

Account - It's a parent for all the other models.

Transaction - Child of a Account model

Income, Expense, Transfer - Child of Transaction model

In Account model, you can create the common fields that's used across all the models you mentioned. It's a primary table, which has columns like account number, name, mobile and address of a account holder.

The Transaction model should have the Foreignkey reference of a Account model, it should have account_id, and other columns which needed in transation table.

Income, Expense and Transfer, these models should have a reference of transaction table like transaction_id and other needed stuffs.

So, when you want to make a transfer of a amount, you would probably add some entry in Transfer table. Before that you should have a account.

Now imagine, you have a account and you want to transfer some amount, it should have all the steps as below: When you transfer the amount, it will generate one transaction_id, for that you need to make an entry in Transation table, which should refer the account holder details.

After making an entry in Transaction table, against that transaction_id you need to make an entry in transfer table. Whenever you need to access the transfer details, you would get transaction_id and account details as it is a primary for your table.

class Account(models.Model):
    account_number = models.IntegerField(null=False)
    account_type = models.CharField(max_length=100, null=True)
    account_holder = models.CharField(max_length=100, null=True)


class Transaction(models.Model):
    account = models.ForeignKey(Account, related_name='account', on_delete=models.CASCADE)
    transation_number = models.IntegerField(null=False)


class Income(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    total_income = models.IntegerField(null=False)


class Expense(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    total_expense = models.IntegerField(null=False)


class Transfer(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    transfer_amount = models.IntegerField(null=False)
  • Why the foreign keys in the Transfer, Expense and Income tables are referring to Account table? I am still confused on how to organize all the transactions and read them all at once efficiently. Thank you. – Umer Maqsood Apr 18 '21 at 08:06