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.

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field
Returns:an instance of a form field for this preference, with the correct configuration (widget, initial value, validators...)
field_class = None

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

Example:
from django import forms

class MyPreferenceType(BasePreferenceType):
    field_class = forms.CharField
field_kwargs = {}

Additional kwargs to be passed to the form field.

Example:
class MyPreference(StringPreference):

    field_kwargs = {
        'required': False,
        'initial': 'Hello there'
    }
get_api_additional_data()[source]

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

get_api_field_data()[source]

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

get_field_kwargs()[source]

Return a dict of arguments to use as parameters for the field class instianciation.

This will use field_kwargs as a starter, and use sensible defaults for a few attributes:

  • instance.verbose_name for the field label
  • instance.help_text for the field help text
  • instance.widget for the field widget
get_initial()[source]
Returns:initial data for form field from field_attribute[‘initial’] or default
serializer = None

A serializer class (see dynamic_preferences.serializers)

validate(value)[source]

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.

Example:
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.

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

A preference type that stores a date.

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

A preference type that stores a datetime.

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

A preference type that stores a decimal.Decimal.

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

A preference type that stores a timdelta.

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

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

Example:
from django.core.files.uploadedfile import SimpleUploadedFile

@registry.register
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'

manager['blog__logo'].delete()
get_file_storage()[source]

Override this method if you want to use a custom storage

serializer

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

serializer_class

alias of FileSerializer

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

A preference type that stores a float.

dynamic_preferences.types.IntPreference

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.

Example:
from myapp.blog.models import BlogEntry

@registry.register
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

Note

You should provide either the queryset or model attribute

field_class

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.

dynamic_preferences.types.create_deletion_handler(preference)[source]

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