Preference types

You’ll find here the final, concrete classes of preferences you can use in your own project.

class dynamic_preferences.types.BasePreferenceType(registry=None)[source]

Used as a base for all other preference classes. You should subclass this one if you want to implement your own preference.


Used only to represent a preference value using Rest Framework

Returns:an instance of a form field for this preference, with

the correct configuration (widget, initial value, validators...)

field_attributes = {}

Those attributes are passed to as kwargs field_class when setup_field() is called

field_class = None

A form field that will be used to display and edit the preference use a class, not an instance.

from django import forms

class MyPreferenceType(BasePreferenceType):
    field_class = forms.CharField

Additional data to serialize for use on front-end side, for example


Field data to serialize for use on front-end side, for example will include choices available for a choice field

Returns:initial data for form field, from field_attribute[‘initial’], _default_field_attribute[‘initial’] or default
serializer = None

A serializer class (see dynamic_preferences.serializers)


Create an actual instance of field_class Override this method if needed


Used to implement custom cleaning logic for use in forms and serializers. The method will be passed as a validator to the preference form field.

def validate(self, value):
if value == ‘42’:
raise ValidationError(‘Wrong value!’)
class dynamic_preferences.types.BooleanPreference(registry=None)[source]

A preference type that stores a boolean.

class dynamic_preferences.types.ChoicePreference(registry=None)[source]

A preference type that stores a string among a list of choices.

choices = ()

Expects the same values as for django forms.ChoiceField.

class MyChoicePreference(ChoicePreference):
    choices = [
        ('c', 'Carrot'),
        ('t', 'Tomato'),
class dynamic_preferences.types.DecimalPreference(registry=None)[source]

A preference type that stores a decimal.Decimal.

class dynamic_preferences.types.FilePreference(registry=None)[source]

A preference type that stores a a reference to a model.

from django.core.files.uploadedfile import SimpleUploadedFile

class Logo(FilePreference):
    section = Section('blog')
    name = 'logo'

logo = SimpleUploadedFile(
    "logo.png", b"file_content", content_type="image/png")
manager['blog__logo'] = logo

# accessing the value will return a FieldFile object, just as
# django.db.models.FileField
assert manager['blog__logo'].read() == b'file_content'


Override this method if you want to use a custom storage


The serializer need additional data about the related preference to upload file to correct directory


alias of FileSerializer

class dynamic_preferences.types.FloatPreference(registry=None)[source]

A preference type that stores a float.


alias of IntegerPreference

class dynamic_preferences.types.IntegerPreference(registry=None)[source]

A preference type that stores an integer.

class dynamic_preferences.types.LongStringPreference(registry=None)[source]

A preference type that stores a string, but with a textarea widget.

class dynamic_preferences.types.ModelChoicePreference(*args, **kwargs)[source]

A preference type that stores a reference to a model instance.

from import BlogEntry

class FeaturedEntry(ModelChoicePreference):
    section = Section('blog')
    name = 'featured_entry'
    queryset = BlogEntry.objects.filter(status='published')

blog_entry = BlogEntry.objects.get(pk=12)
manager['blog__featured_entry'] = blog_entry

# accessing the value will return the model instance
assert manager['blog__featured_entry'].pk == 12


You should provide either the queryset or model attribute


alias of ModelChoiceField

model = None

Which model class to link the preference to. You can skip this if you define the queryset attribute.

queryset = None

A queryset to filter available model instances.

class dynamic_preferences.types.StringPreference(registry=None)[source]

A preference type that stores a string.


Will generate a dynamic handler to purge related preference on instance deletion