MongoEngine / MongoEngine/mongoengine
to_json does not honor primary_key field
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the to_json entry point and reproduce the documented Device example with hostname as the primary_key. Determine how serialization currently represents _id and verify that the completed behavior returns the configured field name without breaking existing output expectations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100