Skip to content

Collectible

deby edited this page Jul 14, 2020 · 5 revisions

↑ 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

Customizing the collectibles' MagiCollection

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.

Using a different selector to item

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)

Multiple collectibles for a single collection

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

  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