简体   繁体   English

如何构造一个 Django model 纯粹由 ticker 关联的其他模型制成的?

[英]How to construct a Django model that is purely made from other models related by ticker?

How to make up a "parent" model that is solely composed of other models?如何组成一个单独由其他模型组成的“父”model? I am currently thinking to use foreignkey relationships but I don't believe this is the right way to do so.我目前正在考虑使用外键关系,但我认为这不是正确的方法。

class BalanceSheet(models.Model):
    ticker = models.ForeignKey(
        Stock, on_delete=models.CASCADE, related_name="balance_sheets"
    )

    assets = models.ForeignKey(Assets, on_delete=models.CASCADE)
    liab_and_stockholders_equity = models.ForeignKey(LiabAndStockholdersEquity, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.ticker} Balance Sheet"

class Assets(model.Model):
    ticker = models.ForeignKey(
    Stock, on_delete=models.CASCADE, related_name="assets")
    balance_sheet = ????????????????????????????



class Assets(model.Model):
    ticker = models.ForeignKey(
    Stock, on_delete=models.CASCADE, related_name="liab_and_stockholders_equity")
    balance_sheet = ??????????????????????????

I think what you need to do is create a model for the parent table, and there you will have child1 = ForeignKey(Child1, on_delete=models.CASCADE) child2 = ForeignKey(Child2, on_delete=models.CASCADE) and so on for each child, you want to have a connection to.我认为你需要做的是为父表创建一个 model,在那里你将有child1 = ForeignKey(Child1, on_delete=models.CASCADE) child2 = ForeignKey(Child2, on_delete=models.CASCADE)等等孩子,你想有一个连接。 No need to write this in child models, only in the parent.无需在子模型中编写,只需在父模型中即可。

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

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