更改数据库模式更好吗?

时间:2011-01-16 20:11:56

标签: database django google-app-engine nosql

我正在使用django构建一个Web应用程序。我使用postgresql作为db。即使我在本地运行应用程序,应用程序代码也变得非常混乱(我的begginer技能是一个重要因素)并且速度很慢。

这是我的models.py文件的摘录:

REPEATS_CHOICES = (
    (NEVER, 'Never'),
    (DAILY, 'Daily'),
    (WEEKLY, 'Weekly'),
    (MONTHLY, 'Monthly'),
    ...some more...
)

class Transaction(models.Model):
    name = models.CharField(max_length=30)
    type = models.IntegerField(max_length=1, choices=TYPE_CHOICES) # 0 = 'Income' , 1 = 'Expense'
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    date = models.DateField(default=date.today)
    frequency = models.IntegerField(max_length=2, choices=REPEATS_CHOICES)
    ends = models.DateField(blank=True, null=True)
    active = models.BooleanField(default=True)
    category = models.ForeignKey(Category, related_name='transactions', blank=True, null=True)
    account = models.ForeignKey(Account, related_name='transactions')

问题在于日期,频率和结束。通过这些信息,我可以知道交易发生的所有日期,并使用它来填充现金流量表。以这种方式做事涉及创建许多结构(字典,列表和元组)并对它们进行大量迭代。也许有一种非常简单的方法可以用实际的模式来解决这个问题,但我无法实现。

我认为,如果在创建事务时我可以保存数据库中的所有日期,那么应用程序将更容易编码。我不知道这是否可能,或者它是否是一个好主意。

我正在阅读一本关于谷歌应用引擎和数据存储区的多值属性的书。你怎么看待解决我的问题?

编辑:我不知道PickleField。我现在正在阅读它,也许我可以使用它来存储所有事务的datetime对象。

Edit2:这是我的cashflow2视图的摘录(对于可怕的代码感到抱歉):

def cashflow2(request, account_name="Initial"):

if account_name == "Initial":
    uri = "/cashflow/new_account"
    return HttpResponseRedirect(uri)     
month_info = {}
cat_info = {}
m_y_list = [] # [(month,year),]
trans = []
min, max = [] , []

account = Account.objects.get(name=account_name, user=request.user)
categories = account.categories.all() 
for year in range(2006,2017):
    for month in range(1,13):
        month_info[(month, year)] = [0, 0, 0]
        for cat in categories:
            cat_info[(cat, month, year)] = 0

previous_months = 1 # previous months from actual
next_months = 5
dates_list = month_year_list(previous_month, next_months) # Returns [(month,year)] from the requested range
m_y_list = [(date.month, date.year) for date in month_year_list(1,5)]
min, max = dates_list[0], dates_list[-1]
INCOME = 0
EXPENSE = 1
ONHAND = 2
transacs_in_dates = []
txs = account.transactions.order_by('date')

for tx in txs:
    monthyear = ()
    monthyear = (tx.date.month, tx.date.year)
    if tx.frequency == 0:
        if tx.type == 0:
            month_info[monthyear][INCOME] += tx.amount
            if tx.category:
                cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
        else:
            month_info[monthyear][EXPENSE] += tx.amount
            if tx.category:
                cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
        if monthyear in lista_m_a:
            if tx not in transacs_in_dates:
                transacs_in_dates.append(tx)
    elif tx.frequency == 4: # frequency = 'Monthly'
        months_dif = relativedelta.relativedelta(tx.ends, tx.date).months
        if tx.ends.day < tx.date.day:
            months_dif += 1
        years_dif = relativedelta.relativedelta(tx.ends, tx.date).years
        dif = months_dif + (years_dif*12)
        dates_range = dif + 1
        for i in range(dates_range):
            dt = tx.date+relativedelta.relativedelta(months=+i)
            if (dt.month, dt.year) in m_y_list:
                if tx not in transacs_in_dates:
                    transacs_in_dates.append(tx)
            if tx.type == 0:
                month_info[(fch.month,fch.year)][INCOME] += tx.amount
                if tx.category:
                    cat_info[(tx.category, fch.month, fch.year)] += tx.amount
            else:
                month_info[(fch.month,fch.year)][EXPENSE] += tx.amount
                if tx.category:
                    cat_info[(tx.category, fch.month, fch.year)] += tx.amount

import operator
thelist = []
thelist = sorted((my + tuple(v) for my, v in month_info.iteritems()),
             key = operator.itemgetter(1, 0))
thelistlist = []
for atuple in thelist:
    thelistlist.append(list(atuple))
for i in range(len(thelistlist)):
    if i != 0:
        thelistlist[i][4] = thelistlist[i-1][2] - thelistlist[i-1][3] + thelistlist[i-1][4]
list = []
for el in thelistlist:
    if (el[0],el[1]) in lista_m_a:
        list.append(el)

transactions = account.transactions.all()

cats_in_dates_income = []
cats_in_dates_expense = []
for t in transacs_in_dates:
    if t.category and t.type == 0:
        if t.category not in cats_in_dates_income:
            cats_in_dates_income.append(t.category)
    elif t.category and t.type == 1:
        if t.category not in cats_in_dates_expense:
            cats_in_dates_expense.append(t.category)

cat_infos = []
for k, v in cat_info.items():
    cat_infos.append((k[0], k[1], k[2], v))

1 个答案:

答案 0 :(得分:1)

取决于App Engine的相关性。附:如果您想在Google数据存储区中存储腌制对象和JSON对象,请查看以下两个代码段:

http://kovshenin.com/archives/app-engine-json-objects-google-datastore/ http://kovshenin.com/archives/app-engine-python-objects-in-the-google-datastore/

另请注意,Google数据存储区是一个非关系型数据库,因此您可能还有其他问题需要重构代码才能切换到该数据库。

干杯,祝你好运!

相关问题