在Django中使用信号创建相关模型的另一个模型

时间:2019-05-14 05:46:16

标签: python django django-models django-signals

假设模型为:

ACCOUNT_UNDER = (
    ('Assets', 'Assets'),
    ('Bank', 'Bank'),
    ('Cash-in-Hand', 'Cash-in-Hand'),
)

class Account(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    under = models.CharField(max_length=100, choices=ACCOUNT_UNDER)

    class MPTTMeta:
        order_insertion_by = ['name']

class JournalVoucher(models.Model):
    date = models.DateField()


class Transaction(models.Model):
    voucher = models.ForeignKey(JournalVoucher, on_delete=models.CASCADE, blank=True, null=True)
    date = models.DateField(default=datetime.date.today)

class TransactionEntry(models.Model):
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, blank=True, null=True)
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="transaction_entries")
    debit = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    credit = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)

class Ledger(models.Model):
    account_name = models.CharField(max_length=254)

class LedgerEntry(models.Model):
    ledger = models.ForeignKey(Ledger, on_delete=models.CASCADE, blank=True, null=True)
    date = models.DateField()
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="debit_account")
    debit = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    credit = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    balance = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)

在django管理员中,

#using django-nested-admin
from django.contrib import admin

import nested_admin

class TransactionEntryInline(nested_admin.NestedTabularInline):
    model = TransactionEntry
    extra = 0


class TransactionInline(nested_admin.NestedStackedInline):
    model = Transaction
    inlines = [TransactionEntryInline]
    extra = 0


@admin.register(JournalVoucher)
class JournalVoucherAdmin(nested_admin.NestedModelAdmin):
    inlines = [TransactionInline]

会计示例:

Jan. 10   Creative Advertising obtained a loan of Rs. 20,000 from the bank.

日记凭证

Date   Account   Debit  Credit
Jan 10 Cash      20,000 
       Bank Loan        20,000

分类帐:

  • 银行贷款帐户
Date Account Amount Date   Account Amount
                        Jan 10 Cash        20,000
  • 现金帐户
Date   Account Amount Date   Account Amount
Jan 10 Bank Loan   20,000                       

问题

  • 保存后保存信号的顺序为: JournalVoucher > Transaction > TransactionEntry
  • TransactionEntry可能是现金或银行贷款,但一次都不能同时显示信号
  • 但是要创建分类帐,两个交易条目都是必需的。在银行贷款帐户中,需要银行以及现金帐户

目标:

  • 我想在日记凭证保存时自动创建分类帐及其各自的分类帐条目

0 个答案:

没有答案