2

I have a model Transaction Type that has credit_account and debit_account fields. Both fields are foreign keys from the Account model. Is it good idea to have them like the way I have implemented below?

class TransactionType(models.Model):
    name = models.CharField(max_length=255)
    organization = models.IntegerField(null=False, blank=False)  
    credit_account = models.ForeignKey(Account)
    debit_account = models.ForeignKey(Account)

Account Model

class Account(MPTTModel):

    TYPES = Choices(
        ("AS", "asset", "Asset"),  # Eg. Cash in bank
        ("LI", "liability", "Liability"),  # Eg. Loans, bills paid after the fact (in arrears)
        ("IN", "income", "Income"),  # Eg. Sales, housemate contributions
        ("EX", "expense", "Expense"),  # Eg. Office supplies, paying bills
        ("EQ", "equity", "Equity"),  # Eg. Money from shares
        ("TR", "trading", "Currency Trading"),  # Used to represent currency conversions
        ("OR", "operating_revenues", "Operating Revenues"),
        ("OX", "operating_expenses", "Operating Expenses"),
        ("NR", "nonoperating_revenues", "Non-Operating Revenues"),
        ("NX", "nonoperating_expenses", "Non-Operating Expenses"),
    )


    uuid = SmallUUIDField(default=uuid_default(), editable=False)
    name = models.CharField(max_length=255,blank=True, null=True)
    parent = TreeForeignKey(
        "self",
        null=True,
        blank=True,
        related_name="children",
        db_index=True,
        on_delete=models.CASCADE,
    )
    code = models.CharField(max_length=3, null=True, blank=True)
    full_code = models.CharField(max_length=100, db_index=True, unique=True, null=True, blank=True)
    account_type = models.CharField(max_length=255,choices=TYPES, blank=True)
    # is_bank_account = models.BooleanField(default=False, blank=True,)
    currencies = ArrayField(models.CharField(max_length=255, db_index=True))
    organization = models.IntegerField(null=False, blank=False)

    objects = AccountManager.from_queryset(AccountQuerySet)()
Philip Mutua
  • 6,016
  • 12
  • 41
  • 84

2 Answers2

1

It is generally not a bad idea but you should add a related_name so Django can differentiate between them in related queries

Ken4scholars
  • 6,076
  • 2
  • 21
  • 38
1

You will need to add different related names for both the foreign keys and you are gtg.

Zoie
  • 344
  • 2
  • 9