Reputation: 327
If have a model like:
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()
To create Person class objects I get data from a stored procedure, like:
cursor = connection.cursor()
cursor.callproc('sp_get_person_by_id', {id:1234})
results = cursor.fetchall()
[Person(*row) for row in results]
But "sp_get_person_by_id" returns more fields than the Person class attributes. Therefore "shit happens", error happens because there is fields that don't have attributes to map.
It's possible to just map this attributes? How can I do that?
Thx in advance.
Cheers.
Upvotes: 3
Views: 4530
Reputation: 337
If you know the order of the attributes you're getting back from the stored procedure you can pass them to the Model like this:
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
Upvotes: 2