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

property 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

  • instance.required defined if the value is required or not

  • instance.initial defined if the initial value

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.

field_class

alias of BooleanField

serializer

alias of BooleanSerializer

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'),
    ]
field_class

alias of ChoiceField

get_api_additional_data()[source]

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

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

  • instance.required defined if the value is required or not

  • instance.initial defined if the initial value

serializer

alias of StringSerializer

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.DatePreference(registry=None)[source]

A preference type that stores a date.

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field_class

alias of DateField

serializer

alias of DateSerializer

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

A preference type that stores a datetime.

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field_class

alias of DateTimeField

serializer

alias of DateTimeSerializer

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

A preference type that stores a decimal.Decimal.

field_class

alias of DecimalField

serializer

alias of DecimalSerializer

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

A preference type that stores a timedelta.

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field_class

alias of DurationField

serializer

alias of DurationSerializer

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()
api_repr(value)[source]

Used only to represent a preference value using Rest Framework

default = None

A default value for the preference

field_class

alias of FileField

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

  • instance.required defined if the value is required or not

  • instance.initial defined if the initial value

get_file_storage()[source]

Override this method if you want to use a custom storage

property 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.

field_class

alias of FloatField

serializer

alias of FloatSerializer

dynamic_preferences.types.IntPreference

alias of IntegerPreference

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

A preference type that stores an integer.

field_class

alias of IntegerField

serializer

alias of IntegerSerializer

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

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field_class

alias of ModelChoiceField

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

  • instance.required defined if the value is required or not

  • instance.initial defined if the initial value

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.

serializer_class

alias of ModelSerializer

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

A preference type that stores a reference list to the model instances.

Example:

from myapp.blog.models import BlogEntry

@registry.register
class FeaturedEntries(ModelMultipleChoicePreference):
    section = Section('blog')
    name = 'featured_entries'
    queryset = BlogEntry.objects.all()

blog_entries = BlogEntry.objects.filter(status='published')
manager['blog__featured_entries'] = blog_entries

# accessing the value will return the model queryset
assert manager['blog__featured_entries'] == blog_entries

Note

You should provide either the queryset or model attribute

field_class

alias of ModelMultipleChoiceField

serializer_class

alias of ModelMultipleSerializer

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

A preference type that stores multiple strings among a list of choices.

Example:

@registry.register
class FeaturedEntries(MultipleChoicePreference):
    section = Section('blog')
    name = 'featured_entries'
    choices = [
        ('c', 'Carrot'),
        ('t', 'Tomato'),
    ]

Note

Internally, the selected choices are stored as a string, separated by a separator. The separator defaults to ‘,’. The way this is implemented still is sae also on keys that cotain the separator, but if in doubt, you can still set the separator to any other character.

field_class

alias of MultipleChoiceField

serializer

alias of MultipleSerializer

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.StringPreference(registry=None)[source]

A preference type that stores a string.

field_class

alias of CharField

serializer

alias of StringSerializer

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

A preference type that stores a time.

api_repr(value)[source]

Used only to represent a preference value using Rest Framework

field_class

alias of TimeField

serializer

alias of TimeSerializer

dynamic_preferences.types.create_deletion_handler(preference)[source]

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