Widgets

The list of ingredient choices is quite large, so this module provides lazy loaded widgets which help reducing loading times.

class dj_ingredient_field.widgets.LazyWidget(lazy_endpoint, attrs=None)

Base class for lazy loaded, choice based widgets.

offloads option generation to the client, relies on correct endpoint setup.

__init__(lazy_endpoint, attrs=None)

Creates a new LazyWidget

Args:

lazy_endpoint (str): the relative or absolute url which retrieves json choices (corresponding to the underlying choices) in the following format: { values: […[<db value>, <user readable value>]…]} attrs: the attributes to pass down to the widget html. Defaults to None.

__weakref__

list of weak references to the object (if defined)

class dj_ingredient_field.widgets.LazySelectWidget(lazy_endpoint, attrs=None)

Single option selection widget corresponding exactly to Select from django widgets. Example usage (requires dj_ingredient_field.urls to be included into your urls somewhere):

from django import forms
from dj_ingredient_field.widgets import LazySelectWidget
from django.urls import reverse_lazy

class IngredientQuantityAdminForm(forms.ModelForm):
    class Meta:
        model = IngredientQuantity
        widgets = {
            'ingredient': LazySelectWidget(reverse_lazy('dj_ingredient_field:ingredients'))
        }
        fields = '__all__' # required for Django 3.x
class dj_ingredient_field.widgets.LazySelectMultipleWidget(lazy_endpoint, attrs=None)

WIP