Skip to content

Extras

This section refers to the extras that Saffier offer and can be used in your application without incurring into extra overhead to make it happen.

If you are in this section, you surely read about the auto discovery and how it relates with the way Saffier handles and manages migrations for you.

But, what if you simply would like to use the shell or any related command offered by Saffier that does not require migration management?

The current runtime entry point is saffier.Instance(...) plus saffier.monkay.set_instance(...). The deprecated Migrate(...) wrapper still exists for compatibility, but it is no longer the preferred bootstrap. There are also cases where migration management is not needed at all, for example a project using reflect models.

A project using reflect models means migrations are managed externally and Saffier only needs to reflect those tables back into your code, so the answer is still no.

So how can you still use those features without coupling your setup to the compatibility wrapper? Use SaffierExtra.

SaffierExtra

This is the object you want to use when you do not need Saffier to manage migrations and still want Saffier tooling such as the shell.

How does it work

It follows the same active-instance runtime direction as the rest of the current Saffier bootstrap.

Let us use Ravyn again as an example like we did for the tips and tricks.

#!/usr/bin/env python
"""
Generated by 'ravyn-admin createproject'
"""

import os
import sys
from pathlib import Path

from ravyn import Ravyn, Include

import saffier
from saffier import Database, Registry, SaffierExtra

database = Database("sqlite:///db.sqlite")
registry = Registry(database)


class CustomModel(saffier.Model):
    name = saffier.CharField(max_length=255)
    email = saffier.EmailField(max_length=255)

    class Meta:
        registry = registry


def build_path():
    """
    Builds the path of the project and project root.
    """
    Path(__file__).resolve().parent.parent
    SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

    if SITE_ROOT not in sys.path:
        sys.path.append(SITE_ROOT)
        sys.path.append(os.path.join(SITE_ROOT, "apps"))


def get_application():
    """
    This is optional. The function is only used for organisation purposes.
    """

    app = Ravyn(
        routes=[Include(namespace="my_project.urls")],
    )

    SaffierExtra(app=app, registry=registry)
    return app


app = get_application()

And that is it, you can use any tool that does not relate with migrations in your application.

Warning

Be aware of the use of this special class in production! It is advised not to use it there.

Note

For now, besides the migrations and the shell, Saffier does not offer any extra tools but there are plans to add more extras in the future and SaffierExtra is the way to go for that setup.