Skip to content

Store JSON

deby edited this page Nov 25, 2021 · 2 revisions

↑ Parent: MagiModel utils

← Previous: Translate fields values in multiple languages

If you need to store more complex data structure in your models, you can use JSON fields provided by MagiCircles. It's generally not recommended, so you may want to try to rethink your database if you end up having to use this feature.

${PROJECT}/models.py:

from magi.item_model import MagiModel

class Card(MagiModel):
    ...
    j_skill = models.TextField(null=True)

You can then manipulate the JSON like so:

card = models.Card.objects.get(id=1)

# Access the JSON value as python
print card.skill # { 'name': 'Turtle swap', 'values': [1, 2] }
print card.skill['name'] # 'Turtle swap'

# Save a new JSON value
card.save_j({
    'name': 'Crazy whip',
    'values': [1, 68, 0],
    'template': 'Whips the opponent {0} time(s), then gets {1} points and loses {2} hp.',
})

This example features a dictionary, but any JSON serializable value would work as well.

There is no specific support for form or item details for JSON fields, so they're mostly meant to be used internally and not be visible to users - though of course it's possible to configure your forms and views to use them anyway.

ℹ︎ See also: Store dictionaries

I. Introduction

II. Tutorials

  1. Collections
    1. MagiModel
    2. MagiCollection
    3. MagiForm
    4. MagiFiltersForm
    5. MagiFields
  2. Single pages
  3. Configuring the navbar

III. References

IV. Utils

V. Advanced tutorials

VI. More

Clone this wiki locally