Skip to content

Abstract models

deby edited this page Feb 14, 2023 · 7 revisions

↑ Parent: References

← Previous: MagiCollection settings

Abstract models are models that don't exist in the database, but you can use as a base for your own models using inheritance.

MagiCircles provides some abstract models for you:

BaseAccount

It's highly recommended to use this class to create the required account_model for your project (see ACCOUNT_MODEL in the required Website settings).

# models.py
from magi.abstract_models import BaseAccount

class Account(BaseAccount):
    ...
ℹ︎ See also: BaseAccount model

AccountAsOwnerModel

If your model doesn't have an owner field but has an account field which you're using to retrieve the owner, you can use AccountAsOwnerModel. It will cache the required owner id for you, avoiding any extra query or join when MagiCircles will use check for permissions.

# models.py
from magi.abstract_models import AccountAsOwnerModel

class CollectibleCard(AccountAsOwnerModel):
    ...
ℹ︎ See also: MagiModel, section "Have an owner"

CacheOwner

Most of the time, you will not need to display any information related to the owner of an item.

If you do, it's recommended to use an internal cache. In that case, you don't need to write the code for the cache yourself, you can just inherit from CacheOwner:

${PROJECT}/models.py

from magi.abstract_models import CacheOwner

class Card(CacheOwner):
    ...

You will then be able to use the details of the owner without extra query or join by doing so:

card = models.Card.objects.get(id=1)
print card.cached_owner.username

No extra field is added besides caching fields.

cached_owner is a property attr dict that contains the following:

  • pk, id
  • username
  • email
  • item_url, ajax_item_url, full_item_url, http_item_url, share_url
  • preferences
    • i_status, status, t_status
    • twitter
    • color, localized_color, hex_color, rgb_color, css_color

MobileGameAccount

${PROJECT}/models.py:

from magi.abstract_models import MobileGameAccount

class Account(MobileGameAccount):
    ...

In addition to all the fields in BaseAccount model, the following fields are also available:

Field name Type Description Default
friend_id CharField(100) Most games provide a friend ID or friend code you can share with other players to follow them in-game. Only accepts Numbers None
show_friend_id BooleanField Show or hide that code to other players? True
accept_friend_requests NullBooleanField Indicates to other players wether you'll accept their friend request or not True
i_play_with Save choices values as integer rather than strings Choices are: Thumbs, Fingers, Index, Hand, Other None
i_os Save choices values as integer rather than strings Operating System. Choices are: iOs, Android None
device CharField(150) The model of your device. Example: Nexus 5, iPhone 4, iPad 2, ...
screenshot ImageField See "Self-service verifications" in BaseAccount model None
_thumbnail_screenshot ImageField See "Self-service verifications" in BaseAccount model and MagiModel images and files None
level_on_screenshot_upload PositiveIntegerField See "Self-service verifications" in BaseAccount model None
is_hidden_from_leaderboard BooleanField See "Self-service verifications" in BaseAccount model False
is_playground BooleanField See "Self-service verifications" in BaseAccount model False

Events

${PROJECT}/models.py:

from magi.abstract_models import BaseEvent

class Event(BaseEvent):
    ...

If your events have "versions", you can also use getBaseEventWithVersions.

ℹ︎ See also: Events

Event participations

${PROJECT}/models.py:

from magi.abstract_models import BaseEventParticipation

class EventParticipation(BaseEventParticipation):
    ...
ℹ︎ See also: Events

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