1

I am getting payload which included key like

{

"id": UUID,
"phone_number": "+141 98324 2434",
"organization": "xyz",
"property":{
    "name": "abc",
    "email": "xyz",
    "address": {
        "street": "6th St",
        "city": "San Francisco",
        "state": "CA",
        "postalCode": "94103",
        "country": "USA"
    },
    "age":14,
}

}

my model looks like this:

from jsonfield import JSONField
organization = models.ForeignKey(Organization, on_delete=models.DO_NOTHING)
phone_number = models.CharField(max_length=16, unique=True, db_index=True)
tags = models.TextField(blank=True, null=True)
property = JSONField(default={}, blank=True, null=True)

Now while storing property(its dynamic) in my djongo DB, i want it to save as object(dict) data type so that later i can query using customer.object.filter(property__age=14).

Right now its getting stored as string hence i m not able to query my property field.

here is some what is looks like in mongodb.

id: UUID
phone_numer: "xyz"
organization: "xyz"
property:"{"name":"abc","age":14}"

How should i store property as object in mongodb?

  • check this post: https://stackoverflow.com/questions/10445176/how-to-write-a-query-to-get-find-value-in-a-json-field-in-django – tstoev Aug 26 '20 at 10:33
  • Hi @tstoev I have through the most but it didnt satisfy my needs. Either they are using postgres.JSONField which is giving me SQLDecodeError since i m using djongo, or they are talking about jsonfield.JSONfield() which i have used but not working when i try to fetch property__age= 14 or with any other keys in object. – ajay kumar prajapati Aug 27 '20 at 16:28

0 Answers0