简体   繁体   English

创建作为基础模型子集的单独模型/表

[英]Create separate models/tables that are subset of base model

I have a Transaction model that contains the list of all transactions from my bank account. 我有一个交易模型,其中包含银行帐户中所有交易的列表。 I want to create "sub" models that are just different categories of transactions. 我想创建只是不同类别的交易的“子”模型。 For instance, an Income model that just contains the deposits or a Bill model that contains the transactions that are from bills. 例如,仅包含存款的收入模型或包含来自票据的交易的票据模型。 The closest I could find were proxy models but either they are too complex for me to understand or I'm totally wrong anyway. 我能找到的最接近的是代理模型,但是它们要么太复杂以至于我无法理解,要么我完全错了。

class Transaction(models.Model):
    date = models.DateField(default=datetime.date.today)
    description = models.CharField(max_length=100)
    category = models.CharField(max_length=100)
    amount = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return self.description + ' ' + str(self.amount)

class Income(Transaction):
    class Meta:
        proxy = True

Is this possible? 这可能吗?

您可以将category字段与选择一起使用,而不是继承。

Apart from what @rakyi said if we need to do model operation based on that then there is another possible way to use django-polymorphic link . 除了@rakyi所说的之外,如果我们需要基于该模型执行模型操作,那么还有另一种使用django-polymorphic link的可能方法。 This also support DRF so it's very powerful and useful. 这也支持DRF因此它非常强大且有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM