Saving an unknown object type into database in Django
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have the following model:
class Parameter(models.Model):
par_type = = models.CharField(max_length=30)
value = models.TextField()
Majority of the time the value
field will store a number (value=3.2118
). But sometimes I want to assign a list of other object IDs (like value="[32, 22, 45]"
) or a string object (like value="pass"
), etc. I will use value
field in functions like the ones below based on the value of par_type
:
def foo(par): # par is a Parameter object.
return float(par.value) / 2
def bar(par):
# Some code to convert a par to a list of objects
object_list = get_objects(par)
# Return a random object from list
return random.choice(object_list)
I don't want to write a piece of code for every possible object type. Ideally there is one decompose()
function to use everywhere. I thought of saving object as pickle
or JSON
type (saw it from here). But I couldn't figured out how to do it. I'm using MySQL db.
mysql json django pickle
add a comment |
I have the following model:
class Parameter(models.Model):
par_type = = models.CharField(max_length=30)
value = models.TextField()
Majority of the time the value
field will store a number (value=3.2118
). But sometimes I want to assign a list of other object IDs (like value="[32, 22, 45]"
) or a string object (like value="pass"
), etc. I will use value
field in functions like the ones below based on the value of par_type
:
def foo(par): # par is a Parameter object.
return float(par.value) / 2
def bar(par):
# Some code to convert a par to a list of objects
object_list = get_objects(par)
# Return a random object from list
return random.choice(object_list)
I don't want to write a piece of code for every possible object type. Ideally there is one decompose()
function to use everywhere. I thought of saving object as pickle
or JSON
type (saw it from here). But I couldn't figured out how to do it. I'm using MySQL db.
mysql json django pickle
add a comment |
I have the following model:
class Parameter(models.Model):
par_type = = models.CharField(max_length=30)
value = models.TextField()
Majority of the time the value
field will store a number (value=3.2118
). But sometimes I want to assign a list of other object IDs (like value="[32, 22, 45]"
) or a string object (like value="pass"
), etc. I will use value
field in functions like the ones below based on the value of par_type
:
def foo(par): # par is a Parameter object.
return float(par.value) / 2
def bar(par):
# Some code to convert a par to a list of objects
object_list = get_objects(par)
# Return a random object from list
return random.choice(object_list)
I don't want to write a piece of code for every possible object type. Ideally there is one decompose()
function to use everywhere. I thought of saving object as pickle
or JSON
type (saw it from here). But I couldn't figured out how to do it. I'm using MySQL db.
mysql json django pickle
I have the following model:
class Parameter(models.Model):
par_type = = models.CharField(max_length=30)
value = models.TextField()
Majority of the time the value
field will store a number (value=3.2118
). But sometimes I want to assign a list of other object IDs (like value="[32, 22, 45]"
) or a string object (like value="pass"
), etc. I will use value
field in functions like the ones below based on the value of par_type
:
def foo(par): # par is a Parameter object.
return float(par.value) / 2
def bar(par):
# Some code to convert a par to a list of objects
object_list = get_objects(par)
# Return a random object from list
return random.choice(object_list)
I don't want to write a piece of code for every possible object type. Ideally there is one decompose()
function to use everywhere. I thought of saving object as pickle
or JSON
type (saw it from here). But I couldn't figured out how to do it. I'm using MySQL db.
mysql json django pickle
mysql json django pickle
asked Nov 24 '18 at 3:20
HBatHBat
1,09611630
1,09611630
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can try like this using BinaryField:
import pickle
class Parameter(models.Model):
_value = models.BinaryField()
def set_data(self, data):
self._value = pickle.dumps(data)
def get_data(self):
return pickle.loads(self._value)
value = property(get_data, set_data)
Usage:
In: b = Foo.objects.create(value=1)
In: b.value
Out: 1
In: b = Foo.objects.create(value={1:1,2:2})
In: b.value
Out: {1: 1, 2: 2}
In: b = Foo.objects.create(value=[1,1])
In: b.value
Out: [1, 1]
FYI, you can't store pass
because it is not a Python Object, but its part of syntax. Instead of pass, consider using None
.
Worked great! Just to be clear on your last line, I was planning to set a string value likeFoo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot doFoo.objects.create(value=pass)
?
– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454891%2fsaving-an-unknown-object-type-into-database-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try like this using BinaryField:
import pickle
class Parameter(models.Model):
_value = models.BinaryField()
def set_data(self, data):
self._value = pickle.dumps(data)
def get_data(self):
return pickle.loads(self._value)
value = property(get_data, set_data)
Usage:
In: b = Foo.objects.create(value=1)
In: b.value
Out: 1
In: b = Foo.objects.create(value={1:1,2:2})
In: b.value
Out: {1: 1, 2: 2}
In: b = Foo.objects.create(value=[1,1])
In: b.value
Out: [1, 1]
FYI, you can't store pass
because it is not a Python Object, but its part of syntax. Instead of pass, consider using None
.
Worked great! Just to be clear on your last line, I was planning to set a string value likeFoo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot doFoo.objects.create(value=pass)
?
– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
add a comment |
You can try like this using BinaryField:
import pickle
class Parameter(models.Model):
_value = models.BinaryField()
def set_data(self, data):
self._value = pickle.dumps(data)
def get_data(self):
return pickle.loads(self._value)
value = property(get_data, set_data)
Usage:
In: b = Foo.objects.create(value=1)
In: b.value
Out: 1
In: b = Foo.objects.create(value={1:1,2:2})
In: b.value
Out: {1: 1, 2: 2}
In: b = Foo.objects.create(value=[1,1])
In: b.value
Out: [1, 1]
FYI, you can't store pass
because it is not a Python Object, but its part of syntax. Instead of pass, consider using None
.
Worked great! Just to be clear on your last line, I was planning to set a string value likeFoo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot doFoo.objects.create(value=pass)
?
– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
add a comment |
You can try like this using BinaryField:
import pickle
class Parameter(models.Model):
_value = models.BinaryField()
def set_data(self, data):
self._value = pickle.dumps(data)
def get_data(self):
return pickle.loads(self._value)
value = property(get_data, set_data)
Usage:
In: b = Foo.objects.create(value=1)
In: b.value
Out: 1
In: b = Foo.objects.create(value={1:1,2:2})
In: b.value
Out: {1: 1, 2: 2}
In: b = Foo.objects.create(value=[1,1])
In: b.value
Out: [1, 1]
FYI, you can't store pass
because it is not a Python Object, but its part of syntax. Instead of pass, consider using None
.
You can try like this using BinaryField:
import pickle
class Parameter(models.Model):
_value = models.BinaryField()
def set_data(self, data):
self._value = pickle.dumps(data)
def get_data(self):
return pickle.loads(self._value)
value = property(get_data, set_data)
Usage:
In: b = Foo.objects.create(value=1)
In: b.value
Out: 1
In: b = Foo.objects.create(value={1:1,2:2})
In: b.value
Out: {1: 1, 2: 2}
In: b = Foo.objects.create(value=[1,1])
In: b.value
Out: [1, 1]
FYI, you can't store pass
because it is not a Python Object, but its part of syntax. Instead of pass, consider using None
.
edited Nov 24 '18 at 8:03
answered Nov 24 '18 at 7:58
ruddraruddra
16.8k42951
16.8k42951
Worked great! Just to be clear on your last line, I was planning to set a string value likeFoo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot doFoo.objects.create(value=pass)
?
– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
add a comment |
Worked great! Just to be clear on your last line, I was planning to set a string value likeFoo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot doFoo.objects.create(value=pass)
?
– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
Worked great! Just to be clear on your last line, I was planning to set a string value like
Foo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot do Foo.objects.create(value=pass)
?– HBat
Nov 24 '18 at 13:24
Worked great! Just to be clear on your last line, I was planning to set a string value like
Foo.objects.create(value="pass")
which worked fine. I believe you meant that I cannot do Foo.objects.create(value=pass)
?– HBat
Nov 24 '18 at 13:24
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
Yes. I guess you are doing it right.
– ruddra
Nov 24 '18 at 13:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454891%2fsaving-an-unknown-object-type-into-database-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown