Django REST: How to change default field type of a ModelSerializer class
I am trying to overwrite serializer's data
field, which is a CharField
,like this:
from jsonfield import JSONField
class ActivitySerializer(serializers.ModelSerializer):
data = JSONField()
class Meta:
model = Activity
fields = ("date_added", "start_date", "data", "number", "athlete")
But when I try to print the representation of the serializer -print(repr(my_serializer))
- the type of field data
remains unchanged:
ActivitySerializer(<Activity: START DATE: 2018-11-19 06:08:49+00:00, ID: 1973222369, DATE ADDED: 2018-11-20 18:07:32.818798+00:00>):
date_added = DateTimeField(read_only=True)
start_date = DateTimeField(required=False)
data = CharField(style={'base_template': 'textarea.html'})
number = IntegerField(required=False)
athlete = PrimaryKeyRelatedField(queryset=Athlete.objects.all(), required=False)
What am I doing wrong?
python django api django-rest-framework
add a comment |
I am trying to overwrite serializer's data
field, which is a CharField
,like this:
from jsonfield import JSONField
class ActivitySerializer(serializers.ModelSerializer):
data = JSONField()
class Meta:
model = Activity
fields = ("date_added", "start_date", "data", "number", "athlete")
But when I try to print the representation of the serializer -print(repr(my_serializer))
- the type of field data
remains unchanged:
ActivitySerializer(<Activity: START DATE: 2018-11-19 06:08:49+00:00, ID: 1973222369, DATE ADDED: 2018-11-20 18:07:32.818798+00:00>):
date_added = DateTimeField(read_only=True)
start_date = DateTimeField(required=False)
data = CharField(style={'base_template': 'textarea.html'})
number = IntegerField(required=False)
athlete = PrimaryKeyRelatedField(queryset=Athlete.objects.all(), required=False)
What am I doing wrong?
python django api django-rest-framework
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
1
Change its type fromCharField
toJSONField
– barciewicz
Nov 20 '18 at 18:47
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
2
try to useserializers.JSONField()
instead ofjsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46
add a comment |
I am trying to overwrite serializer's data
field, which is a CharField
,like this:
from jsonfield import JSONField
class ActivitySerializer(serializers.ModelSerializer):
data = JSONField()
class Meta:
model = Activity
fields = ("date_added", "start_date", "data", "number", "athlete")
But when I try to print the representation of the serializer -print(repr(my_serializer))
- the type of field data
remains unchanged:
ActivitySerializer(<Activity: START DATE: 2018-11-19 06:08:49+00:00, ID: 1973222369, DATE ADDED: 2018-11-20 18:07:32.818798+00:00>):
date_added = DateTimeField(read_only=True)
start_date = DateTimeField(required=False)
data = CharField(style={'base_template': 'textarea.html'})
number = IntegerField(required=False)
athlete = PrimaryKeyRelatedField(queryset=Athlete.objects.all(), required=False)
What am I doing wrong?
python django api django-rest-framework
I am trying to overwrite serializer's data
field, which is a CharField
,like this:
from jsonfield import JSONField
class ActivitySerializer(serializers.ModelSerializer):
data = JSONField()
class Meta:
model = Activity
fields = ("date_added", "start_date", "data", "number", "athlete")
But when I try to print the representation of the serializer -print(repr(my_serializer))
- the type of field data
remains unchanged:
ActivitySerializer(<Activity: START DATE: 2018-11-19 06:08:49+00:00, ID: 1973222369, DATE ADDED: 2018-11-20 18:07:32.818798+00:00>):
date_added = DateTimeField(read_only=True)
start_date = DateTimeField(required=False)
data = CharField(style={'base_template': 'textarea.html'})
number = IntegerField(required=False)
athlete = PrimaryKeyRelatedField(queryset=Athlete.objects.all(), required=False)
What am I doing wrong?
python django api django-rest-framework
python django api django-rest-framework
edited Nov 20 '18 at 18:57
barciewicz
asked Nov 20 '18 at 18:29
barciewiczbarciewicz
628312
628312
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
1
Change its type fromCharField
toJSONField
– barciewicz
Nov 20 '18 at 18:47
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
2
try to useserializers.JSONField()
instead ofjsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46
add a comment |
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
1
Change its type fromCharField
toJSONField
– barciewicz
Nov 20 '18 at 18:47
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
2
try to useserializers.JSONField()
instead ofjsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
1
1
Change its type from
CharField
to JSONField
– barciewicz
Nov 20 '18 at 18:47
Change its type from
CharField
to JSONField
– barciewicz
Nov 20 '18 at 18:47
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
2
2
try to use
serializers.JSONField()
instead of jsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46
try to use
serializers.JSONField()
instead of jsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46
add a comment |
0
active
oldest
votes
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%2f53399321%2fdjango-rest-how-to-change-default-field-type-of-a-modelserializer-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53399321%2fdjango-rest-how-to-change-default-field-type-of-a-modelserializer-class%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
What do you mean by "overwrite serializer's data field " ?
– Amine Messaoudi
Nov 20 '18 at 18:42
1
Change its type from
CharField
toJSONField
– barciewicz
Nov 20 '18 at 18:47
I guess it says CharFiels because it expects data to be a string, by default. But when you see it in the browser, you will see json data.
– Amine Messaoudi
Nov 20 '18 at 19:00
I will see JSON data, but with backslashes escaping double qoutes. This is becasue JSON is stored as string in model field , and then it is serialized again to string through serializers CharField. But I found solution to this by creating custom serializer field - my question is about someting else:)
– barciewicz
Nov 20 '18 at 19:29
2
try to use
serializers.JSONField()
instead ofjsonfield.JSONField()
– JPG
Nov 21 '18 at 1:46