I am currently working on developing a database and api system where users can create a portfolio which contains a list of coins. I am using django and I searched everywhere but i kept seeing foreign keys but im not sure thats what I need in this situation.
我目前正在开发一个数据库和api系统,用户可以在其中创建一个包含硬币列表的文件夹。我在用django,我到处找,但是我一直在找外键,但我不确定在这种情况下这是不是我需要的。
I want two models, one for portfolios which a user will be able to query on, and another coin model which the user will be able to also query on. However in the portfolio there should be a list of coins.
我想要两个模型,一个用于用户可以查询的投资组合,另一个用于用户也可以查询的投币模型。但是在投资组合中应该有一个硬币清单。
I know how to do this in Java using objects but not sure the method in django.
我知道如何使用对象在Java中实现这一点,但不确定django中的方法。
Here is my model class:
这是我的模特班:
from django.db import models
class Portfolio(models.Model):
name = models.CharField(max_length=250)
def __str__(self):
return self.name
class Coin(models.Model):
name = models.CharField(max_length=100)
symbol = models.CharField(max_length=5)
price = models.DecimalField(max_digits=20, decimal_places=9)
info = models.TextField()
website = models.TextField()
rank = models.IntegerField()
def __str__(self):
return self.name + " - " + self.symbol
Now i would ideally have something like coins = list of Coins model
if I was using java to make the objects, but since this is for a database and in Django, im not sure how I should link the two.
现在,如果我使用java来创建对象,理想情况下,我应该有类似coins = list of coins模型这样的东西,但是由于这是针对数据库和Django的,所以我不知道应该如何链接这两个对象。
I've seen related objects but did not understand the explanations for my issue.
我见过相关的物体,但不明白我的问题的解释。
How should I go about setting up these models?
我应该如何建立这些模型?
Thanks.
谢谢。
1 个解决方案
#1
2
It sounds like you want to have a number of Portfolio
objects each of which can have varying investments in Coin
objects. In this case, you'd want to use a ManyToManyField:
听起来你想要有很多投资组合对象,每个都可以在投币对象上有不同的投资。在这种情况下,你需要使用一个ManyToManyField:
class Portfolio(models.Model):
name = models.CharField(max_length=250)
coins = models.ManyToManyField(Coin)
The database would then store the two dimensional table of which Portfolio holds which coin.
然后,数据库将存储投资组合中包含哪个硬币的二维表。
However an alternate approach you could try is to create an object that separately represents the investment:
不过,您可以尝试的另一种方法是创建一个单独表示投资的对象:
class Investment(models.Model):
portfolio = models.ForeignKey(Portfolio)
coin = models.ForeignKey(Coin)
bought = models.DateTimeField() # date the investment was made
sold = models.DateTimeField() # date the investment was sold
amount = models.DecimalField() # number of coins held
You could then add a property to Portfolio:
然后你可以向投资组合中添加一个属性:
class Portfolio(models.Model):
name = models.CharField(max_length=250)
@property
def coins(self):
return Investment.objects.filter(portfolio=self)
In this way you can not only keep track of which portfolio holds which coins, buy also the entire historical positions too.
这样,你不仅可以跟踪哪个投资组合持有哪些硬币,还可以购买整个历史头寸。
#1
2
It sounds like you want to have a number of Portfolio
objects each of which can have varying investments in Coin
objects. In this case, you'd want to use a ManyToManyField:
听起来你想要有很多投资组合对象,每个都可以在投币对象上有不同的投资。在这种情况下,你需要使用一个ManyToManyField:
class Portfolio(models.Model):
name = models.CharField(max_length=250)
coins = models.ManyToManyField(Coin)
The database would then store the two dimensional table of which Portfolio holds which coin.
然后,数据库将存储投资组合中包含哪个硬币的二维表。
However an alternate approach you could try is to create an object that separately represents the investment:
不过,您可以尝试的另一种方法是创建一个单独表示投资的对象:
class Investment(models.Model):
portfolio = models.ForeignKey(Portfolio)
coin = models.ForeignKey(Coin)
bought = models.DateTimeField() # date the investment was made
sold = models.DateTimeField() # date the investment was sold
amount = models.DecimalField() # number of coins held
You could then add a property to Portfolio:
然后你可以向投资组合中添加一个属性:
class Portfolio(models.Model):
name = models.CharField(max_length=250)
@property
def coins(self):
return Investment.objects.filter(portfolio=self)
In this way you can not only keep track of which portfolio holds which coins, buy also the entire historical positions too.
这样,你不仅可以跟踪哪个投资组合持有哪些硬币,还可以购买整个历史头寸。