我有两个代表一对多关系的模型。InterestRateCurve 对象可以有许多 InterestRate 对象。问题是,当我有一个创建两条曲线的函数时,每条曲线都有一个速率,第二条曲线的速率会覆盖第一条曲线的速率。所以,我最终在数据库中有两行曲线和一行利率。当我执行一个保存一条曲线和一个速率的程序,然后执行一个单独的程序以保存另一条曲线的速率时,一切都很好。
我使用 Python 2.7 和 Django 1.4。
楷模:
class InterestRate(models.Model):
type = models.CharField(max_length=20)
term = modelFields.tgTimePeriodField(max_length=20)
numTerms = models.IntegerField()
mid = models.FloatField()
curve = models.ForeignKey('InterestRateCurve')
class Meta:
unique_together = ('type', 'term', 'numTerms', 'curve')
def __unicode__(self):
return "%s/%s/%s/%s" % (self.type, self.term, self.numTerms, self.curve)
def __str__(self):
return "%s/%s/%s/%s/%s" % (self.type, self.term, self.numTerms, self.mid, self.curve)
class InterestRateCurve(models.Model):
ccy = modelFields.tgCurrencyField(max_length=20)
index = models.CharField(max_length=20)
term = modelFields.tgTimePeriodField(max_length=20)
numTerms = models.IntegerField()
asOf = modelFields.tgDateField()
cvid = models.CharField(max_length=20, blank=True, default='')
#rates needs to be explicitly maintained inside class for convenience
rates = []
class Meta:
unique_together = ('ccy', 'index', 'term', 'numTerms', 'asOf', 'cvid')
def __unicode__(self):
return "%s/%s/%s/%s/%s/%s" % (self.ccy, self.index, self.term, \
self.numTerms, self.asOf, self.cvid)
def save(self):
try:
curve = InterestRateCurve.objects.get(ccy=self.ccy, index=self.index,\
term=self.term, numTerms=self.numTerms,\
asOf=self.asOf, cvid=self.cvid)
#Delete all rates before saving the new rates
oldRates = curve.interestrate_set.all()
for oldRate in oldRates:
oldRate.delete()
for rate in self.rates:
rate.curve = curve
rate.save()
except InterestRateCurve.DoesNotExist:
super(InterestRateCurve, self).save()
curve = InterestRateCurve.objects.get(ccy=self.ccy, index=self.index,\
term=self.term, numTerms=self.numTerms,\
asOf=self.asOf, cvid=self.cvid)
for rate in self.rates:
rate.curve = curve
rate.save()
def addRate(self, rate):
rate.curve = self
exists = False
for r in self.rates:
if r.type == rate.type:
if r.term == rate.term:
if r.numTerms == rate.numTerms:
exists = True
continue
if exists == False:
self.rates.append(rate)
以下代码工作正常:
date = tgDate(month=9,day=12,year=2011)
curve = InterestRateCurve(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=self.asOf, cvid='TESTSAVE2CURVES')
curve.save()
curve = InterestRateCurve.objects.get(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date, cvid='TESTSAVE2CURVES')
rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01, curve=curve)
curve.addRate(rate)
curve.save()
此执行导致保存一条曲线和一种速率。
在单独的执行中,我运行:
date = tgDate(month=9,day=13,year=2011) #DATE DIFFERS
curve = InterestRateCurve(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date, cvid='TESTSAVE2CURVES')
curve.save()
curve = InterestRateCurve.objects.get(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date, cvid='TESTSAVE2CURVES')
rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01)
curve.addRate(rate)
curve.save()
此执行导致另一条曲线和另一个速率被保存。
但是,以下执行无法正常工作:
def testSave2Curves(self):
date = tgDate(month=9,day=12,year=2011)
curve = InterestRateCurve(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date, cvid='TESTSAVE2CURVES')
curve.save()
curve = InterestRateCurve.objects.get(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date, cvid='TESTSAVE2CURVES')
rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01, curve=curve)
curve.addRate(rate)
curve.save()
date1 = tgDate(month=9,day=13,year=2011)
curve1 = InterestRateCurve(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date1, cvid='TESTSAVE2CURVES')
curve1.save()
curve1 = InterestRateCurve.objects.get(ccy='USD', index='LIBOR', term='M',
numTerms=3, asOf=date1, cvid='TESTSAVE2CURVES')
rate1 = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01)
curve1.addRate(rate1)
curve1.save()
这导致保存了两条曲线,但只保存了一个速率。似乎第二个速率会覆盖先前保存的第一个速率。