我有兩個模型表示一對多的關係。 InterestRateCurve對象可以有許多InterestRate對象。 問題是,當我有一個函數創建兩條曲線,其中每條曲線有一個速率時,第二條曲線的速率會覆蓋第一條曲線的速率。所以,我最終在數據庫中有兩行曲線和一行速率。 當我執行一個程序保存一個曲線和一個速率,然後執行一個單獨的程序,保存另一個速率的曲線,那就沒有問題。在保存兩個對象時,Django的ForeignKey關係沒有正確保存
我使用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()
這導致在挽救了兩個曲線,但只有一個速度保存。看起來第二種匯率會覆蓋先前保存的第一種匯率。
修復格式。 – rantanplan
對不起,爲什麼是二分率模型,'rate1',而不是'InterestRate(term ='M',numTerms = 1,type ='Deposit',mid = 0.01,curve = curve1)? – LSerni
lserni,你是對的。我複製/粘貼時發生錯誤。它得到了回答和解決。 –