-
Notifications
You must be signed in to change notification settings - Fork 11
Collectible
↑ Parent: MagiCollection
← Previous: Views
To make a collection collectible, you need to provide a MagiModel. The model should always have either an owner
or an account
foreign key, allowing users to collect them. It is also possible to use another foreign key as an owner, but it will not show up on users profiles by default.
Example of model:
`
from magi.models import AccountAsOwnerModel
from magi.abstract_models import AutoImageFromParent
class CollectibleCard(AccountAsOwnerModel, AutoImageFromParent):
collection_name = 'collectiblecards'
account = models.ForeignKey(Account, related_name='collectedcards')
card = models.ForeignKey(Card, related_name='collectiblecards')
...
AutoImageFromParent
is optional. It will use the image of the parent item as the image for the collectible. In this example, doing collectedcard.image
will give you the same thing as doing collectedcard.card.image
.
Then specify this model in the collection you want to make collectible with that model:
from magi.magicollections import MagiCollection
from . import models
class CardCollection(MagiCollection):
collectible = models.CollectibleCard
You can customize the MagiCollection of the collectible itself:
from magi.magicollections import MagiCollection
from . import models
def to_CollectibleCardCollection(cls):
class _CollectibleCardCollection(cls):
icon = 'world'
return _CollectibleCardCollection
class CardCollection(MagiCollection):
collectible = models.CollectibleCard
def collectible_to_class(self, model_class):
return to_CollectibleCardCollection(super(CardCollection, self).collectible_to_class(model_class))
You can also customize collectible collections MagiForm by doing so:
# in forms.py
def to_CollectibleCardForm(cls):
class _CollectibleCardForm(cls.form_class):
class Meta(cls.form_class.Meta):
optional_fields = ('something',)
return _CollectibleCardForm
# in magicollections.py
from . import forms
def to_CollectibleCardCollection(cls):
class _CollectibleCardCollection(cls):
form_class = forms.to_CollectibleCardForm(cls)
return _CollectibleCardCollection
When customizing your collectible's MagiCollection, keep in mind that Item types are not compatible and shouldn't be used in a collectible's MagiCollection.
The selector to the item foreign key inside the collectible item model will be inferred from the model name (Card
model -> card
) but you can specify it within the collectible item model:
class StarterCard(AccountAsOwnerModel):
collection_name = 'startercard'
selector_to_collected_item = 'starter'
starter = models.ForeignKey(Card)
You may provide multiple collectibles for one collection. For example, you may want to allow users to save the cards they own and their favorite cards. To do so, you can provide a list to collectible. The name of the collections will be infered from collection_name
variable within the model.
from magi.magicollections import MagiCollection
from . import models
class CardCollection(MagiCollection):
collectible = [
models.CollectibleCard,
models.FavoriteCard,
]
In that case, you can customize the MagiCollection for each separately like so:
from magi.magicollections import MagiCollection
from . import models
def to_CollectibleCardCollection(cls):
class _CollectibleCardCollection(cls):
icon = 'world'
return _CollectibleCardCollection
def to_FavoriteCardCollection(cls):
class _FavoriteCardCollection(cls):
icon = 'star'
return _FavoriteCardCollection
class CardCollection(MagiCollection):
collectible = [
models.CollectibleCard,
models.FavoriteCard,
]
def collectible_to_class(self, model_class):
cls = super(CardCollection, self).collectible_to_class(model_class)
if model_class.collection_name == 'favoritecard':
return to_FavoriteCardCollection(cls)
return to_CollectibleCardCollection(cls)
→ Next: CuteForm
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