Choose the models¶
Three ways to put a model on djox. Most projects only need the first.
| Way | For |
|---|---|
DJOX_MODELS |
Every model, or the ones you list, User and other libraries' models included. No code changes |
djox.Manager() |
One model whose code you own |
djox.Model |
One model whose code you own, its asave() and adelete() included |
DJOX_MODELS¶
# settings.py
INSTALLED_APPS = [..., "djox"]
DJOX_MODELS = "__all__" # the default: every model
# DJOX_MODELS = ["auth", "shop.Order"] # or some apps and models
# DJOX_MODELS = [] # or none
When Django starts, djox switches each chosen model:
- Its managers (
objectsand any custom one) returndjox.QuerySets. A customQuerySetkeeps its own methods (Shelf.objects.wide()still works). - Its related managers (
user.groups.aadd(...)) run on djox's threads. - Its
asave(),adelete()andarefresh_from_db()becomedjox.Model's, unless the model defines its own.
What doesn't change:
- The model's code, tables and migrations. A switched manager keeps its class
name, so
makemigrationsand type checkers see the model as before. - A model that already uses
djox.Managerordjox.Model. - Models on a database whose
ENGINEisn't djox's: they keep Django's behavior.
djox.Manager¶
import djox
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
pages = models.IntegerField()
objects = djox.Manager()
- Works without
"djox"inINSTALLED_APPS. - A custom
QuerySetcan subclassdjox.QuerySet, and useMyQuerySet.as_manager().
djox.Model¶
A model's own asave(), adelete() and arefresh_from_db() don't go
through its manager. Inherit from djox.Model to run them on djox's threads
too (and asave() of a new object on djox itself, like acreate()):
class Author(djox.Model):
name = models.CharField(max_length=100)
author = Author(name="Frank Herbert")
await author.asave()
- It also sets
objects = djox.Manager(). - It is abstract (no table), so it changes no migration.