Array/Jsonb update field: psycopg2.ProgrammingError
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Using Django 2.1/ python 3.6 I'm looking to update a field on an existing model.
The field isof the following type:
from django.contrib.postgres.fields import (
ArrayField,
JSONField
)
waypoints = ArrayField(
JSONField(
default=list,
null=True,
blank=True
),
size=None,
null=True,
blank=True
)
And the field will store data in the format:
[
{'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0},
{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998},
{'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}
]
I am acutely aware of the fact that this is not good practice, and that the better method would be to use a foreign key with another model, but my hands are tied.
My code is:
scheduler_response = {
"result": {
"time": 23,
"total_consumption": 98117,
"waypoints": [
{
"lat": 42.315119,
"lon": -3.213883,
"date": "2018-08-12 09:15:00",
"direction": 95.45,
"rpm": 3998,
"cumulative_consumption": 0
},
{
"lat": 51.763023,
"lon": 7.376109,
"date": "2018-08-19 03:30:00",
"direction": 45.76,
"rpm": 7823,
"cumulative_consumption": 44298
},
{
"lat": 63.123334,
"lon": 13.433918,
"date": "2018-08-23 11:00:00",
"direction": 123,
"rpm": 0,
"cumulative_consumption": 98117
}
]
}
}
scheduler_waypoints = scheduler_response.get('result').get('waypoints')
job_updates = {}
job_updates['waypoints'] = scheduler_waypoints
print(type(job_updates['waypoints']))
job_updates['total_consumption'] = scheduler_response.get('total_consumption')
serialized_job = JobSerializer(active_job, data=job_updates, partial=True)
if serialized_job.is_valid():
print('is_valid')
print(serialized_job.validated_data)
serialized_job.save()
print('FINISHED')
My terminal output is as follows:
<class 'list'>
is_valid
OrderedDict([('total_consumption', None), ('waypoints', [{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998}, {'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}, {'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0}])])
Note that although the serializer is validated, the serializer cannot be saved ('FINISHED' is not printed).
I am getting the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "waypoints" is of type jsonb but expression is of type text
LINE 1: ...d" = 1, "total_consumption" = NULL, "waypoints" = ARRAY['{"l...
^
HINT: You will need to rewrite or cast the expression.
Why is this? What am I doing wrong (aside from not using a foreign key and another model)?
django django-serializer
add a comment |
Using Django 2.1/ python 3.6 I'm looking to update a field on an existing model.
The field isof the following type:
from django.contrib.postgres.fields import (
ArrayField,
JSONField
)
waypoints = ArrayField(
JSONField(
default=list,
null=True,
blank=True
),
size=None,
null=True,
blank=True
)
And the field will store data in the format:
[
{'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0},
{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998},
{'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}
]
I am acutely aware of the fact that this is not good practice, and that the better method would be to use a foreign key with another model, but my hands are tied.
My code is:
scheduler_response = {
"result": {
"time": 23,
"total_consumption": 98117,
"waypoints": [
{
"lat": 42.315119,
"lon": -3.213883,
"date": "2018-08-12 09:15:00",
"direction": 95.45,
"rpm": 3998,
"cumulative_consumption": 0
},
{
"lat": 51.763023,
"lon": 7.376109,
"date": "2018-08-19 03:30:00",
"direction": 45.76,
"rpm": 7823,
"cumulative_consumption": 44298
},
{
"lat": 63.123334,
"lon": 13.433918,
"date": "2018-08-23 11:00:00",
"direction": 123,
"rpm": 0,
"cumulative_consumption": 98117
}
]
}
}
scheduler_waypoints = scheduler_response.get('result').get('waypoints')
job_updates = {}
job_updates['waypoints'] = scheduler_waypoints
print(type(job_updates['waypoints']))
job_updates['total_consumption'] = scheduler_response.get('total_consumption')
serialized_job = JobSerializer(active_job, data=job_updates, partial=True)
if serialized_job.is_valid():
print('is_valid')
print(serialized_job.validated_data)
serialized_job.save()
print('FINISHED')
My terminal output is as follows:
<class 'list'>
is_valid
OrderedDict([('total_consumption', None), ('waypoints', [{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998}, {'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}, {'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0}])])
Note that although the serializer is validated, the serializer cannot be saved ('FINISHED' is not printed).
I am getting the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "waypoints" is of type jsonb but expression is of type text
LINE 1: ...d" = 1, "total_consumption" = NULL, "waypoints" = ARRAY['{"l...
^
HINT: You will need to rewrite or cast the expression.
Why is this? What am I doing wrong (aside from not using a foreign key and another model)?
django django-serializer
add a comment |
Using Django 2.1/ python 3.6 I'm looking to update a field on an existing model.
The field isof the following type:
from django.contrib.postgres.fields import (
ArrayField,
JSONField
)
waypoints = ArrayField(
JSONField(
default=list,
null=True,
blank=True
),
size=None,
null=True,
blank=True
)
And the field will store data in the format:
[
{'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0},
{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998},
{'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}
]
I am acutely aware of the fact that this is not good practice, and that the better method would be to use a foreign key with another model, but my hands are tied.
My code is:
scheduler_response = {
"result": {
"time": 23,
"total_consumption": 98117,
"waypoints": [
{
"lat": 42.315119,
"lon": -3.213883,
"date": "2018-08-12 09:15:00",
"direction": 95.45,
"rpm": 3998,
"cumulative_consumption": 0
},
{
"lat": 51.763023,
"lon": 7.376109,
"date": "2018-08-19 03:30:00",
"direction": 45.76,
"rpm": 7823,
"cumulative_consumption": 44298
},
{
"lat": 63.123334,
"lon": 13.433918,
"date": "2018-08-23 11:00:00",
"direction": 123,
"rpm": 0,
"cumulative_consumption": 98117
}
]
}
}
scheduler_waypoints = scheduler_response.get('result').get('waypoints')
job_updates = {}
job_updates['waypoints'] = scheduler_waypoints
print(type(job_updates['waypoints']))
job_updates['total_consumption'] = scheduler_response.get('total_consumption')
serialized_job = JobSerializer(active_job, data=job_updates, partial=True)
if serialized_job.is_valid():
print('is_valid')
print(serialized_job.validated_data)
serialized_job.save()
print('FINISHED')
My terminal output is as follows:
<class 'list'>
is_valid
OrderedDict([('total_consumption', None), ('waypoints', [{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998}, {'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}, {'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0}])])
Note that although the serializer is validated, the serializer cannot be saved ('FINISHED' is not printed).
I am getting the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "waypoints" is of type jsonb but expression is of type text
LINE 1: ...d" = 1, "total_consumption" = NULL, "waypoints" = ARRAY['{"l...
^
HINT: You will need to rewrite or cast the expression.
Why is this? What am I doing wrong (aside from not using a foreign key and another model)?
django django-serializer
Using Django 2.1/ python 3.6 I'm looking to update a field on an existing model.
The field isof the following type:
from django.contrib.postgres.fields import (
ArrayField,
JSONField
)
waypoints = ArrayField(
JSONField(
default=list,
null=True,
blank=True
),
size=None,
null=True,
blank=True
)
And the field will store data in the format:
[
{'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0},
{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998},
{'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}
]
I am acutely aware of the fact that this is not good practice, and that the better method would be to use a foreign key with another model, but my hands are tied.
My code is:
scheduler_response = {
"result": {
"time": 23,
"total_consumption": 98117,
"waypoints": [
{
"lat": 42.315119,
"lon": -3.213883,
"date": "2018-08-12 09:15:00",
"direction": 95.45,
"rpm": 3998,
"cumulative_consumption": 0
},
{
"lat": 51.763023,
"lon": 7.376109,
"date": "2018-08-19 03:30:00",
"direction": 45.76,
"rpm": 7823,
"cumulative_consumption": 44298
},
{
"lat": 63.123334,
"lon": 13.433918,
"date": "2018-08-23 11:00:00",
"direction": 123,
"rpm": 0,
"cumulative_consumption": 98117
}
]
}
}
scheduler_waypoints = scheduler_response.get('result').get('waypoints')
job_updates = {}
job_updates['waypoints'] = scheduler_waypoints
print(type(job_updates['waypoints']))
job_updates['total_consumption'] = scheduler_response.get('total_consumption')
serialized_job = JobSerializer(active_job, data=job_updates, partial=True)
if serialized_job.is_valid():
print('is_valid')
print(serialized_job.validated_data)
serialized_job.save()
print('FINISHED')
My terminal output is as follows:
<class 'list'>
is_valid
OrderedDict([('total_consumption', None), ('waypoints', [{'lat': 42.315119, 'lon': -3.213883, 'date': '2018-08-12 09:15:00', 'direction': 95.45, 'rpm': 3998}, {'lat': 51.763023, 'lon': 7.376109, 'date': '2018-08-19 03:30:00', 'direction': 45.76, 'rpm': 7823}, {'lat': 63.123334, 'lon': 13.433918, 'date': '2018-08-23 11:00:00', 'direction': 123, 'rpm': 0}])])
Note that although the serializer is validated, the serializer cannot be saved ('FINISHED' is not printed).
I am getting the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "waypoints" is of type jsonb but expression is of type text
LINE 1: ...d" = 1, "total_consumption" = NULL, "waypoints" = ARRAY['{"l...
^
HINT: You will need to rewrite or cast the expression.
Why is this? What am I doing wrong (aside from not using a foreign key and another model)?
django django-serializer
django django-serializer
edited Nov 16 '18 at 16:08
User632716
asked Nov 16 '18 at 15:52
User632716User632716
2,51911437
2,51911437
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The answer for anyone else looking to commit this kind of attrocity is to use a JSONBField in your model, not a JSONField inside a list field, here are the changes i made to my model:
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField
class Job(models.Model):
waypoints = JSONBField(
default=list,
null=True,
blank=True
)
The code calling the serializer then works fine.
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%2f53341283%2farray-jsonb-update-field-psycopg2-programmingerror%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
The answer for anyone else looking to commit this kind of attrocity is to use a JSONBField in your model, not a JSONField inside a list field, here are the changes i made to my model:
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField
class Job(models.Model):
waypoints = JSONBField(
default=list,
null=True,
blank=True
)
The code calling the serializer then works fine.
add a comment |
The answer for anyone else looking to commit this kind of attrocity is to use a JSONBField in your model, not a JSONField inside a list field, here are the changes i made to my model:
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField
class Job(models.Model):
waypoints = JSONBField(
default=list,
null=True,
blank=True
)
The code calling the serializer then works fine.
add a comment |
The answer for anyone else looking to commit this kind of attrocity is to use a JSONBField in your model, not a JSONField inside a list field, here are the changes i made to my model:
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField
class Job(models.Model):
waypoints = JSONBField(
default=list,
null=True,
blank=True
)
The code calling the serializer then works fine.
The answer for anyone else looking to commit this kind of attrocity is to use a JSONBField in your model, not a JSONField inside a list field, here are the changes i made to my model:
from django.contrib.postgres.fields.jsonb import JSONField as JSONBField
class Job(models.Model):
waypoints = JSONBField(
default=list,
null=True,
blank=True
)
The code calling the serializer then works fine.
answered Nov 16 '18 at 16:26
User632716User632716
2,51911437
2,51911437
add a comment |
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%2f53341283%2farray-jsonb-update-field-psycopg2-programmingerror%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