Model and form fields¶
This package also provides fields for Momotor resource requirements and the
model that the default token store uses. You need the fields only when your
own models or forms work with resource requirements, such as cpu=2.
Store resource requirements on a model¶
ResourcesField stores a
momotor.shared.resources.Resources object in a text column.
Reading the attribute returns a Resources object again:
from django.db import models
from momotor.django.fields import ResourcesField
class WorkerProfile(models.Model):
resources = ResourcesField()
Assign a Resources object, a string such as 'cpu=2', or None.
An empty value reads back as an empty Resources object. In forms and in
the admin, the field shows a text area with one requirement per line.
- class momotor.django.fields.ResourcesField(*args, **kwargs)¶
Model field that stores
momotor.shared.resources.Resourcesas text.Use this field on a model when you need to save a set of resource requirements, such as
cpu=2, and read it back as aResourcesobject. The database column is a text field. The field allows blank values unless you passblank=False. In forms, the field usesResourcesFormFieldwith a text area widget.- formfield(form_class=None, choices_form_class=None, **kwargs)¶
Return the form field for this model field.
The default form field is
ResourcesFormFieldwith aTextareawidget. Passform_class,choices_form_class, or other form field arguments to override these defaults.
- from_db_value(value, expression, connection)¶
Convert a database value with
to_python().
- get_db_prep_value(value, connection, prepared=False)¶
Convert a value for storage in the database.
A
Resourcesobject is stored in its compact string form.Noneand values that are already strings pass through unchanged.- Parameters:
value – The value to store.
connection – The database connection. Not used.
prepared – Whether the value is already prepared. Not used.
- Returns:
The value to store.
- to_python(value)¶
Convert a stored or assigned value to a
Resourcesobject.Noneconverts to an emptyResourcesobject. A string is parsed withmomotor.shared.resources.Resources.from_string().- Parameters:
value – The value to convert.
- Returns:
The converted value.
Accept resource requirements in a form¶
ResourcesFormField is the form field for
the same values. It parses the entered text and reports a validation error
for text that is not a valid resource specification.
- class momotor.django.forms.ResourcesFormField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)¶
Form field that parses text into a
momotor.shared.resources.Resourcesobject.Use this field in a form when the user enters resource requirements as text, such as
cpu=2. Invalid text raises aValidationError. An empty value converts to an emptyResourcesobject. This is the default form field forResourcesField.- prepare_value(value)¶
Format a value for display in the form widget.
A
Resourcesobject is shown in its multiline string form, with one requirement per line.- Parameters:
value – The value to display.
- Returns:
The value passed to the widget.
- to_python(value)¶
Parse the entered text into a
Resourcesobject.- Parameters:
value – The entered value. A
Resourcesobject passes through unchanged.- Returns:
The parsed value.
- Raises:
django.core.exceptions.ValidationError – If the text is not a valid resource specification.
Token model¶
The default token store saves broker session tokens in this model, one row per API key. When this store is in use, the model also appears in the Django admin as a read-only list, so you can check whether a token exists and when it was stored.
- class momotor.django.models.MomotorToken(*args, **kwargs)¶
A broker session token, stored by
ModelTokenStore.There is one row per API key. The
createdfield records when the token was first stored.- exception DoesNotExist¶
- exception MultipleObjectsReturned¶
- class momotor.django.admin.MomotorTokenAdmin(model, admin_site)¶
Read-only admin view of the stored broker session tokens.
This model admin is registered only when the default
ModelTokenStoreis in use.