简体   繁体   English

如何在 Django 中制作多种类型的模型并以相同的名称引用它们?

[英]How to make multiple types of models and refer them with same name in Django?

I am new to Django.我是 Django 的新手。 I want the functionality of Accounts and Transactions.我想要账户和交易的功能。 Account can have three types of transactions.账户可以有三种类型的交易。 Income, Expense and Transfer.收入、费用和转移。 All of them have some common fields among them.它们之间都有一些共同的领域。 But also have some additional fields.但也有一些额外的领域。 I want to know can I make such models so that I can access all the transactions of account.我想知道我可以制作这样的模型,以便我可以访问帐户的所有交易。 And I also want to use them in rest framework.而且我还想在 rest 框架中使用它们。 Is it possible to do so?有可能这样做吗?

from django.db import models

class Account(models.Model):
     name = models.CharField(max_length=50)
     balance = models.DecimalField(max_digits=7, decimal_places=1)
     def __str__(self):
         return self.name


class Transaction(models.Model):
    amount = models.DecimalField(max_digits=7, decimal_places=1)
    title = models.CharField(max_length=200)
    date_created = models.DateTimeField(auto_now=True,auto_now_add=False)
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='account')

    def __str__(self):
         return str(self.amount)


class Income(models.Model):
    source = models.CharField(max_length=200)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')


class Expense(models.Model):
    category = models.CharField(max_length=200)
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')


class Transfer(models.Model):
    to = models.ForeignKey(Account, on_delete= models.CASCADE, related_name='to')
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction')

I have tried to understand your question and I'll answer based on my understanding.我试图理解你的问题,我会根据我的理解来回答。

So your question is like you've to create multiple models(Account, Transaction(Income, Expense, Transfer)) and refer those details(transaction of a account) in a stretch.因此,您的问题就像您必须创建多个模型(帐户、交易(收入、费用、转账))并在一段时间内参考这些详细信息(帐户交易)。

I probably answer your question, You can create 5 different models as you mentioned.我可能会回答您的问题,您可以创建 5 种不同的模型,如您所提到的。

Models :型号

Account - It's a parent for all the other models.帐户- 它是所有其他模型的父级。

Transaction - Child of a Account model交易-账户model 的子账户

Income, Expense, Transfer - Child of Transaction model收入、费用、转移- 交易的子交易model

In Account model, you can create the common fields that's used across all the models you mentioned.在账户 model 中,您可以创建在您提到的所有模型中使用的通用字段。 It's a primary table, which has columns like account number, name, mobile and address of a account holder.这是一个主表,其中包含帐户持有人的帐号、姓名、手机和地址等列。

The Transaction model should have the Foreignkey reference of a Account model, it should have account_id, and other columns which needed in transation table. Transaction model 应该有一个 Account model 的 Foreignkey 引用,它应该有 account_id,以及事务表中需要的其他列。

Income, Expense and Transfer, these models should have a reference of transaction table like transaction_id and other needed stuffs.收入,费用和转移,这些模型应该有事务表的引用,如事务ID和其他需要的东西。

So, when you want to make a transfer of a amount, you would probably add some entry in Transfer table.因此,当您想要进行一笔金额的转账时,您可能会在转账表中添加一些条目。 Before that you should have a account.在此之前,您应该有一个帐户。

Now imagine, you have a account and you want to transfer some amount, it should have all the steps as below: When you transfer the amount, it will generate one transaction_id, for that you need to make an entry in Transation table, which should refer the account holder details.现在想象一下,你有一个账户,你想转账一些金额,它应该有以下所有步骤: 当你转账金额时,它会生成一个 transaction_id,为此你需要在 Transation 表中做一个条目,它应该请参阅帐户持有人的详细信息。

After making an entry in Transaction table, against that transaction_id you need to make an entry in transfer table.在 Transaction 表中创建条目后,针对该 transaction_id,您需要在传输表中创建一个条目。 Whenever you need to access the transfer details, you would get transaction_id and account details as it is a primary for your table.每当您需要访问转账详细信息时,您都会获得 transaction_id 和帐户详细信息,因为它是您的表的主要信息。

class Account(models.Model):
    account_number = models.IntegerField(null=False)
    account_type = models.CharField(max_length=100, null=True)
    account_holder = models.CharField(max_length=100, null=True)


class Transaction(models.Model):
    account = models.ForeignKey(Account, related_name='account', on_delete=models.CASCADE)
    transation_number = models.IntegerField(null=False)


class Income(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    total_income = models.IntegerField(null=False)


class Expense(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    total_expense = models.IntegerField(null=False)


class Transfer(models.Model):
    transaction_id = models.ForeignKey(Account, related_name='transaction', on_delete=models.CASCADE)
    transfer_amount = models.IntegerField(null=False)

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

相关问题 如何在 django 中调用/引用相同模型中的另一个属性 - How can I call/refer to another property in the same models in django 请参阅Django中的View / Template中的多个模型 - Refer to multiple Models in View/Template in Django 如何在 Django 中的同一个查询中查询多个模型? - How to query multiple models in the same query in Django? scrapy - 处理多种类型的项目 - 多个和相关的Django模型,并将它们保存到管道中的数据库 - scrapy - handling multiple types of items - multiple and related Django models and saving them to database in pipelines 如何在 django 模型中为 class 中的多个变量分配相同的值 - How to assign the same value to multiple variables in a class in django models 如何在Flask中设置具有相同名称的模型的多个应用程序? - How to setup multiple apps in Flask that have models with same name? 不同 Django 应用程序中不同模型的相同名称 - The same name for different models in different Django applications 如果模型名称是应用程序名称,如何使Django模型不会将应用程序的名称添加到模型中? - How to make Django models not prepend the app's name to the model if the model name is the app name? 如何在Django模型中制作“group by”? - How make “group by” in Django models? Django具有相同模型但数据不同的多个数据库 - Django multiple databases with same models but different data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM