是否有一种“好”的方式来构建和组织Django模型?

时间:2012-12-12 04:37:02

标签: python django optimization models

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

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

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

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

谢谢。

models.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

2 个答案:

答案 0 :(得分:1)

粗略地看一下你的模特,这表明你有一个非常好的设计。但有几个提及。

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

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

  3. 我将单独留下PEP-8风格的评论,因为这似乎不是您的问题的重点,但是您应该考虑方法中的文档字符串而不是多个单行注释来改进命令行文档和内省

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

    我还会将您的Club模型移到文件的最高位置,这样您就不需要通过'Club'引用它,而只需传递类名。

答案 1 :(得分:0)