Skip to content

Support for JSONField #36

@MightySCollins

Description

@MightySCollins

Hi

I am trying to use the Django models JSONField but because its structure can be changed I am not sure of its properties. Is there anyway I can avoid setting them when using a ObjectField? Or can you think of any other solutions?

Without setting properties like below the following happens

attributes = fields.ObjectField()
"attributes": [
  {
  },
  {
  }
],

You package has saved me so much time, thanks.

Activity

boccheciampe

boccheciampe commented on Nov 21, 2017

@boccheciampe

Hello,

I recently ran into the same issue. A (dirty ?) workaround is to use the prepare_foo function, something like this (in this example, "yourfield" is a relationship object with regular fields and a 'data' jsonfield):

class MyIndex(DocType):
    [...]

    yourfield = fields.ObjectField()

    def prepare_yourfield(self, instance):
        json_keys = [list_of_all_possible_keys_of_the_jsonfield]
        final_list = []

        for yourfield in instance.yourfields.all():
            # regular fields values
            fields_dict = {
                'attr1': yourfield.attr1,
                'attr2: yourfield.attr2,
                [...]
            }
            # flatten the json field values
            for k in keys:
                fields_dict[k] = yourfield.data.get(k)

            final_list.append(fields_dict)
    return final_list

Hope this helps :)

cocoakekeyu

cocoakekeyu commented on Apr 8, 2018

@cocoakekeyu

i try this:

class MyType(DocType):
    content_json = fields.ObjectField()

    def prepare_content_json(self, instance):
        return instance.content_json
andreasnuesslein

andreasnuesslein commented on Nov 28, 2019

@andreasnuesslein
Contributor

so, part of it is in master:

#220

Allan-Nava

Allan-Nava commented on Feb 12, 2020

@Allan-Nava

i try this:

class MyType(DocType):
    content_json = fields.ObjectField()

    def prepare_content_json(self, instance):
        return instance.content_json

it works this solution?

andreasnuesslein

andreasnuesslein commented on Feb 12, 2020

@andreasnuesslein
Contributor

it works this solution?

? yes? did you try it? people are giving a thumbs up

debnet

debnet commented on Jul 15, 2024

@debnet

It works, but for few records only. I want to index about 10 millions records and refresh command takes all my memory and finally crashes. I hope for a real fix because JSONField are ratherly common now in PostgreSQL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @andreasnuesslein@debnet@boccheciampe@cocoakekeyu@MightySCollins

        Issue actions

          Support for JSONField · Issue #36 · django-es/django-elasticsearch-dsl