我的models.py中有这个经理
class ItemManager(models.Manager):
def get_fee(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
SELECT fee
FROM item
WHERE itemID = %d AND item.type = %d
""", [self.myItemID, self.myItemType])
fee = cursor.fetchone()
return fee
和班级
Sample(models.Model):
sampleID = models.AutoField(primary_key=True)
itemID = models.ForeignKey(Item)
item.type = models.ForeignKey(Item)
...
def save(self, *args, **kwargs):
is_new = self.pk is None
super(Sample, self).save(*args, **kwargs)
if is_new:
cd.amount = MonthlyFeeManager()
cd.save()
然后它会产生一个错误:
Cannot convert <myapp.models.ItemManager object at 0xa6f07ec> to Decimal
一般来说,我想在管理器中执行 RAW SQL 查询并使用它从查询中获取结果。我试图搜索,但大多数返回是元组,而不是一个值。