0

I am modelling an accounting system. I have the following models:

class GeneralAccount(MPTTModel):
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    name = models.CharField(max_length=100, verbose_name='Account Name')
    code = models.CharField(max_length=10, verbose_name='Account Code')
    balance = models.DecimalField(max_digits=9, decimal_places=2)

class InventoryAccount(GeneralAccount): 
    description = models.TextField()
    price = models.DecimalField(max_digits=9, decimal_places=2)
    cost = models.DecimalField(max_digits=9, decimal_places=2)
    available_stock = models.DecimalField(max_digits=9, decimal_places=2)
    value =  models.DecimalField(max_digits=9, decimal_places=2)    


class JournalEntry(models.Model):
    transaction_date = models.DateField(verbose_name='Transaction Date', default=django.utils.timezone.now)
    account_from = models.ForeignKey(GeneralAccount, on_delete=models.PROTECT) # THIS IS THE PROBLEM
    account_to = models.ForeignKey(GeneralAccount, on_delete=models.PROTECT) # THIS IS THE PROBLEM
    amount = models.DecimalField(max_digits=9, decimal_places=2, blank=True, null=True)

My problem is in the account_from/account_to fields.

Currently, it is only accepting a GeneralAccount but I also want it to accept an InventoryAccount because essentially it is an MPTTModel/GeneralAccount with just some specialized fields.

The only thing I can think of is to create a separate JournalEntry class for Inventory but I would prefer just to streamline it. Actually, if I could have only one MPTT model (i.e One Account Model) that would be preferrable.

Is there way around this?

P.S. I have checked several accounting github repos but essentially they are not implementing a subsidiary ledger accounts. They are all general ledger accounts. The separate InventoryAccount class is my attempt to implement a subsidiary ledger.

Ben
  • 15
  • 7
  • Hi @AbdulAzizBarkat. Thanks for the link. Yes! This is what I was looking for. Didn't know about contenttype before. Anyhow, here is a solid reference for the contenttype: 1) https://www.youtube.com/watch?v=dqVztASjrpM&t=7s 2) https://simpleisbetterthancomplex.com/tutorial/2016/10/13/how-to-use-generic-relations.html and 3) And of course the documentation: https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations Have a great day ahead! :) – Ben Jul 11 '21 at 15:46

0 Answers0