[Answer]-Why doesn't Django serialize full model relations?

1πŸ‘

βœ…

It is possible using natural key, a custom manager and get_by_natural_key(). Django doc has a full explanation, please read here. I’d rather not copy paste everything here.

By using this method, you can turn the regular serialization like this –

{
    "pk": 1,
    "model": "store.book",
    "fields": {
        "name": "Mostly Harmless",
        "author": 42
    }
}

To this –

{
    "pk": 1,
    "model": "store.book",
    "fields": {
        "name": "Mostly Harmless",
        "author": ["Douglas", "Adams"]
    }
}

Leave a comment