question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Update json field

See original GitHub issue

I have a json field structured like this:

[{"fecha": "31-10-2020 16:00", "doctor": "5d2a1c29-21b9-4a02-846c-171ba65ba0ea", "paciente": {"code": "a4723719-fbd4-4d16-9470-97ea93c3c5dd", "conv": "sebastian muñoz"}}]

Is there a way of update a single attribute of the json field ?..I mean without making a query and extracting the attributes.

return await some_pydantic.from_queryset_single(SomeModel.get(id=id)) <—Not this

something like this…obviously field and attr are examples return await some_pydantic.from_queryset_single(SomeModel.update(id=id).field(jsonField).attr(fecha=‘New Date’))

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
long2icecommented, Nov 1, 2020

OK, this is postgres example:

from tortoise.expressions import F
from pypika.terms import Function

class JsonbSet(Function):
    def __init__(self, field: F, path: str, value: Any, create_if_missing: bool = False):
        super().__init__("jsonb_set", field, path, value, create_if_missing)

json = await JSONFields.create(data_default={"a": 1})
json.data_default = JsonbSet(F("data_default"), "{a}", '3') # in fact '3' is integer 
await json.save()

And the sql is

UPDATE "jsonfields" SET "data_default"=jsonb_set("data_default", '{a}', '3', false) WHERE "id" = 1
0reactions
Master-Y0dacommented, Nov 1, 2020

Ok…I get it. But still is really confusing implement this in tortoise…Could you please add an example ? https://www.postgresql.org/docs/12/functions-json.html

Thank you for your time and predisposition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating JSON Data in PostgreSQL - Aaron Bos
Updating JSON Data in PostgreSQL. If you're storing JSON data in Postgres, you'll eventually need to update it. In this post, we'll talk ......
Read more >
How do I modify fields inside the new PostgreSQL JSON ...
@Andrew, you'd use to_jsonb(substring(column_name, 1, 2)) to convert the column value to jsonb. So, update objects set body=jsonb_set(body, '{name}', to_jsonb( ...
Read more >
postgresql 13 - how to update a property value of a jsonb field?
There are two ways to accomplish this: Simply concatenating the new key/value pair: update the_table set attr = attr || '{"is_default": ...
Read more >
Update Json field via Nifi - Cloudera Community - 321075
Support Questions · 1. Add a new property(keyword_value) in ExtractText and value/expression should be below · 2. Remove space,double quotes from ...
Read more >
How to ask for each field for update in json file. - CodeProject
I'll update. The problem is when I enter some input for change suppose I change the city name then instead of storing the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found