-
Notifications
You must be signed in to change notification settings - Fork 11
Store JSON
↑ 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
→ Next: More model field types
I. Introduction
II. Tutorials
III. References
- Files tree
- Default collections
- Default pages
- Website settings
- MagiCollection settings
- Abstract models
- Abstract collections
- MagiForm settings
- MagiFiltersForm settings
- Single pages settings
IV. Utils
-
MagiModel utils
- MagiModel images and files
- DateTime fields
- BaseAccount model
- Save choices values as integer rather than strings
- Store comma separated values
- Store dictionaries
- Store Markdown texts
- Translate fields values in multiple languages
- Store JSON
- More model field types
- Transform images before saving them
- Check choices at form level instead of model level
- Use an internal cache for foreign keys in models
- Customize views with MagiModel properties
- Events
- Form utils
- Python utils
- Templates utils
- Javascript utils
- Enable and disable existing collections and pages
- Translations
- Page titles and descriptions
- Characters
- Seasons
- Roles and permissions
- Allow your staff team to change configurations
- Custom users preferences and settings
- Background illustrations
- Activities tabs
- Corner popups
VI. More