Django 使用原生SQL

def dictfetchall(cursor):
"将游标返回的结果保存到一个字典对象中"
desc = cursor.description
return [
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
]

class DtsinfoManager(models.Manager):
def indoing(self):
sql = """ select * from alpm_dtsinfo where name = '1' """
cursor = connection.cursor()
cursor.execute(sql)
return dictfetchall(cursor)

class Dtsinfo(models.Model):
name = models.CharField(max_length=40,verbose_name=u'名称')
parent = models.ForeignKey('self',default=None,blank=True,null=True,verbose_name=u'上级分类')
rank = models.IntegerField(default=0,null=True,verbose_name=u'排序')
status = models.IntegerField(default=0,choices=STATUS.items(),verbose_name='状态')

create_time = models.DateTimeField(u'创建时间',auto_now_add=True)
objects = DtsinfoManager()
class Meta:
verbose_name_plural = verbose_name = u'分类'
ordering = ['rank','-create_time']
posted @ 2017-01-09 00:51  aloneOnWorld  阅读(1210)  评论(0编辑  收藏  举报