Skip to content

Support for JSONField #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MightySCollins opened this issue Jul 4, 2017 · 6 comments
Open

Support for JSONField #36

MightySCollins opened this issue Jul 4, 2017 · 6 comments

Comments

@MightySCollins
Copy link

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.

@sabricot sabricot added the bug label Jul 5, 2017
@boccheciampe
Copy link

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
Copy link

i try this:

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

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

@andreasnuesslein
Copy link
Contributor

so, part of it is in master:

#220

@Allan-Nava
Copy link

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
Copy link
Contributor

it works this solution?

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

@debnet
Copy link

debnet commented Jul 15, 2024

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
Projects
None yet
Development

No branches or pull requests

7 participants