gpt4 book ai didi

python - 是否有构建和组织 Django 模型的 'good' 方法?

转载 作者:行者123 更新时间:2023-11-28 18:50:22 24 4
gpt4 key购买 nike

我正在尝试构建一个 Django 应用程序,它可以让小型股票俱乐部轻松管理和查看有关其俱乐部的数据。我遇到的问题是构建优雅且合乎逻辑的模型。我没有找到任何关于构建模型的“好”方法,甚至是开始规划模型的“好”方法的文献,所以我想我会在这里发布我的 models.py 并看看你的想法。

我是否按照逻辑构建了它们?有没有更好的办法?我不太关心代码本身的风格(它最终将符合 PEP-8),因为我关心设计的逻辑和优雅。

为了更好地了解目的是什么,这里有一个非常简单的示例,说明模型包含的数据将生成什么:http://www.bierfeldt.com/takestock/clubs/1/

以下是模型当前外观的流程图设计: Here is a flowchart design of what the models look like currently.

谢谢。

模型.py

from django.db import models


####### Stock Models #######


class Stock(models.Model):

'''A stock whose current_price is updated every minute by a cronned script
running on the server. The current_price updating script gets all Stock objects
and runs Google Finance queries on each Stock

Stock has no relation to a particular owner, for that, see StockInstance model below'''

ticker = models.CharField(max_length=200)
current_price = models.DecimalField(max_digits=20, decimal_places=2)

def __unicode__(self):
return str(self.ticker + ">" + str(self.current_price))


class StockInstance(models.Model):

'''A middle-man model which links a Stock model to an owner (see Club model below.)

A single owner may possess multiple instances of a single stock purchased at different times
ex. December 9, 2012 - Owner buys 20 shares of AAPL at $500
December 13, 2012 - Owner buys 15 shares of AAPL at $482'''

owner = models.ForeignKey('Club')
stock = models.ForeignKey(Stock)

def current_price(self):
#Current Price of relevant stock
return self.stock.current_price

shares = models.IntegerField()
purchase_date = models.DateTimeField()
purchase_price = models.DecimalField(max_digits=20, decimal_places=2)

#if is_open is False, the instance is considered a closed position
is_open = models.BooleanField(default=True)
sell_date = models.DateTimeField(blank=True, null=True)
sell_price = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)

def current_value(self):
#Current value of this stock instance
#ex. $200 Current Price * 10 shares = $2000
return (self.current_price() * self.shares)

def purchase_value(self):
#Purchase value of this stock instance
#ex. $195 Purchase Price * 10 shares = $1950
return (self.purchase_price * self.shares)

def percent_gl(self):
#Percent Gained/Lost
#ex. ($2000 Current Value - $1950 Purchase Value) / ($1950 Purchase Value) = .03 (03%) Gained
return ((self.current_value() - self.purchase_value()) / (self.purchase_value()))

def amount_gl(self):
#Dollar Value Gained/Lost
#ex. $2000 Current Value - $1950 Purchase Value = $50 Gained
return (self.current_value() - self.purchase_value())

def total_percentage(self):
#Percent of Club Value (all club assets including cash) which this stock instance comprises
#ex. $2000 Current Value / $10000 Club Total Assests = .20 (20%)
return (self.current_value() / self.owner.current_value())

def __unicode__(self):
return str(str(self.shares) + str(" of ")+ str(self.stock.ticker))


####### Member Models #######


class Member(models.Model):


'''Members may belong to multiple clubs. The Member model has no relation to a club

See MemberInstance model below'''

name = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

def __unicode__(self):
return self.name


class MemberInstance(models.Model):

'''A middle-man model which links a Member model to an owner (see Club model below.)

A single member may belong to multiple clubs at the same time
ex. John has 5 shares of "Sandstone Investment Club"
John has 15 shares of "Blackwell Investment Firm"'''

owner = models.ForeignKey('Club')
member = models.ForeignKey(Member)
shares = models.DecimalField(max_digits=20, decimal_places=2)

def total_share_value(self):
#Total Dollar value of all of particular member's shares of a club
#ex. Sandstone Investment Club's Share Price is $20 and John has 5 shares
#ex. cont. $20 * 5 shares = $100 value of John's shares in Sandstone Investment Club
return (self.shares * self.owner.current_price())

def total_share_percentage(self):
#Percent of a club that a particular member owns
#ex. John has $100 of Sandstone Investment, Sandstone Investment is worth $1000
#ex. cont. $100 / $1000 = .10 (10%) John owns 10% of Sandstone Investment's Value
return (float(self.total_share_value()) / float(self.owner.current_value()))


####### Club Models #######


class Club(models.Model):
'''A Stock Club

A club has members (MemberInstance) and buys Stocks (StockInstance).

A note on the real-life purpose of stock clubs: Small-tim individual investors often do not have the
buying power to make powerful stock purchases. A single individual may not be able to buy 50 shares
of a stock priced at $500 each. This individual joins a stock club, possibly with friends, family, or co-workers.
The stock club has a number of shares that each member owns some of. The stock CLUB may own shares of many different
STOCKS, but the club only has ONE stock price--its own.'''

name = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
cash = models.DecimalField(max_digits=20, decimal_places=2)

def total_shares(self):
#The number of shares of the club that exist
#ex. John has 6 shares of the club; Bob has 4 shares of the club
#ex. cont. The club has (6+4=9) 10 total shares among its members.
shares = 0
for member in self.memberinstance_set.select_related():
shares = shares + member.shares
return shares

def current_value(self):
#The current value of the club
#The current value of each stock instance plus the club's uninvested cash
#ex. $200 from AAPL StockInstance + $400 from GOOG Instance + $20 cash = $620
value = 0
for stock in self.stockinstance_set.select_related():
if stock.is_open == True:
value = value + stock.current_value()
else:
pass
return (self.cash + value)

def current_price(self):
#The club's current share price
#The current value of the club divided by the total number of shares of the club
#ex. $620 Club Current Value / 10 Total Shares = $62 per share
return (self.current_value() / self.total_shares())

def cash_total_percentage(self):
#Percent of club's current value that is uninvested cash
return ((self.cash) / (self.current_value()))

def __unicode__(self):
return self.name









最佳答案

粗略地看一下您的模型表明您的设计基本不错。不过有几点提及。

  1. MemberInstance(和其他“中间人模型”)通常被命名为 ClubMember,一个描述关系双方的名称。

  2. 您的“中间人模型”是 ManyToMany Models 的示例.您应该在您的模型上定义 ManyToManyField,例如在您的 Club 模型上定义 members = ManyToManyField(Member, through='ClubMember')

我将单独保留 PEP-8 样式注释,因为这似乎不是您问题的重点,但是您应该在方法中考虑文档字符串而不是多个单行注释,以改进命令行文档和自省(introspection).

另请注意,select_related 应包含您希望 django 遵循关系的字段(从 django 1.5 开始)。

我还会将你的 Club 模型移到文件中更高的位置,这样你就不需要通过 'Club' 引用它,而是可以直接传递类姓名。

关于python - 是否有构建和组织 Django 模型的 'good' 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com