Skip to content

to_json does not honor primary_key field #549

@tcgarvin

Description

@tcgarvin

When using MongoEngine to abstract mongo (which is its purpose, I suppose), and when using one of my fields as the primary_key, to_json still returns the _id field. For example, in the below code, I would expect '_id' to be returned to me as 'hostname'.

class Device(Document):
    hostname=StringField(primary_key=True)
    cpu_load_average_15min=IntField()
    last_communication=FloatField()

for device in Device.objects.all():
    son.append(json.loads(device.to_json()))

# son is {"_id": "host3", "last_communication": 1389059449.072472}, {"_id": "host1", "cpu_load_average_15min": 0, "last_communication": 1389380999.455526}]

Obviously I can work around this, but in my opinion the point of an abstaction layer is to abstract away things like '_id'.

Would a pull request be welcome to fix this? I would anticipate adding a keyword argument honor_field_names that, when true, would account changing the _id fieldname. That argument would be False by default, To avoid backwards compatibility problems.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions