[Answer]-Representing many to many model in django as json

1๐Ÿ‘

โœ…

'rules': [r.to_json() for r in self.rules.all()]

0๐Ÿ‘

The way that the dumpdata management command does it with fixtures is to dump an array of the PK values for the objects in the ManyToMany field. The instances themselves are dumped separately.

For example:

 {
    "pk": 365,
    "model": "tree.tree",
    "fields": {
        "label": "Root",
        "subtrees": [
            367,
            368
         ]
 }

Iโ€™d start by looking into that implementation and see what, if anything, you can reuse from it.

๐Ÿ‘คJim K.

Leave a comment