Reputation: 3018
I have a class defined like below
class StatementStatus:
def __init__(self, id, statement, call, intent, score):
self.id = callback_id
self.statement = statement
self.entities = entities
self.intent = intent
self.score = score
def getIntent(self):
return self.intent
def getScore(self):
return self.score
def addIntent(self, intent_val):
self.intent.append(intent_val)
Now I want to store an instance of this class into postgres database.This file is hosted in a django server and I am unable to use django migrations due to some issues in the server.So I need to do it manually.
Below is my django model
class StoreState(models.Model):
context_name = models.CharField(null=True,max_length=100)
state = models.BinaryField(null=True)
Here I want to store the StatementStatus instance in a field called state.
Similarly my postgres table structure looks like below
Column | Type | Modifiers
--------------+------------------------+-----------
id | integer |
context_name | character varying(100) |
state | bytea |
But it doesn't store any entry into the table when I do an operation like below
try:
conv = StoreState.objects.create(
context_name = callback_id,
state=s
)
except Exception as e:
print("unable to save update", e)
But it doesn't store anything into the table.I get the below error.
('unable to save update', TypeError("can't escape instance to binary",))
What am I doing wrong?
Upvotes: 1
Views: 1950
Reputation: 5942
If you want to save your object in your database then use pickle.
import pickle
state = pickle.dump(state) #your state object will be converted to binary
try:
conv = StoreState.objects.create(
context_name = callback_id,
state=s
)
except Exception as e:
print("unable to save update", e)
Upvotes: 1