如果有类似的模型:
class Person(models.Model):
id = models.IntegerField()
first_name = models.CharField(max_length=50)
last_name = models.IntegerField()
birthday = models.DateField()
address = models.CharField(max_length=250)
phone = models.TimeField()
要创建Person类对象,我从存储过程中获取数据,例如:
cursor = connection.cursor()
cursor.callproc('sp_get_person_by_id', {id:1234})
results = cursor.fetchall()
[Person(*row) for row in results]
但是“sp_get_person_by_id”返回的字段多于Person类属性。因此“发生了”,发生错误,因为有些字段没有要映射的属性。
可以只映射这些属性吗?我怎么能这样做?
提前谢谢。干杯。
答案 0 :(得分:1)
如果您知道从存储过程中返回的属性的顺序,您可以将它们传递给模型,如下所示:
cursor = connection.cursor()
cursor.callproc('sp_get_person_by_id', {id:1234})
results = cursor.fetchall()
result_list = []
from row in results:
p = self.model(id=row[0], fist_name=row[1], last_name=row[2], birthday=row[3])
result_list.append(p)
return result_list