Skip to content

Item Types

deby edited this page Jan 25, 2022 · 3 revisions

↑ Parent: MagiCollection

← Previous: Alt views

You may display different forms and URLs to add an item depending on "types".

When you use types on a collection, its model must have a type (ie we should be able to do instance.type), which can be stored in database or returned with @property. It'll be used to retrieve the right form when editing.

For example, let's say we have 3 types of cards: Normal, Rare and Super Rare and we want to use different forms for those. Our collection will look like that:

class CardCollection(MagiCollection):
    ...
    types = {
        'normal': { ... },
        'rare': { ... },
        'superrare': { ... },
    }

For each type, you may specify the following settings in its dictionary:

Key Value Default Example
form_class FormClass to add/edit the item, must take request (make it inherit from FormWithRequest), can be a function that takes request, context and collection required Example
title Localized title of the type type (key) _('Rare')
plural_title Localized plural title of the type title value _('Reports')
image Path of an image displayed near the title of the form that illustrates the type None 'rarity/ur.png'
icon Icon displayed near the title of the form that illustrates the type None 'idolized'

The type will be passed to the formClass when it's initialized, which allows you to reuse the same form class for all your types if you'd like.

It's also possible to check for permissions to add differently based on type by overriding the method check_type_permissions in Add view. The method takes request, context, type as parameters.

→ Next: API

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