Skip to main content

Easily create extra Meta option classes on forms

Project description

Easily add more Metaesque option classes to your Django forms, models and more.

Installing

Install using pip:

pip install django-easyoptions

It works with Django 1.4 and upwards.

Using

To make use of Options in your class, define an Options class, and use one of the metaclass factories provided.

from django import forms
from django.utils import six
from easyoption.options import OptionsBase, form_option_factory


# Define your ``Option``\s container class
class ExtendedOptions(OptionsBase):
    def __init__(self, options):

        # Collect your options, supplying defaults as appropriate
        self.foo = options.pop('foo', None)
        self.frobnicate = options.pop('frobnicate', True)

        # Call the ``super().__init__()``, which ensures all options are
        # consumed.
        super(ExtraOption, self).__init__(options)


# Define your base class, using the options from ``_extendedoptions``
class ExtendedForm(six.with_metaclass(
        form_option_factory(ExtendedOptions),
        forms.Form)):

    def __init__(self, **kwargs):
        super(ExtendedForm, self).__init__(**kwargs)

        if self._extendedoptions.frobnicate:
            self.bar(self._extendedoptions.foo)


# Extend your base class, and define the ``ExtendedOptions`` for options
# for this specific implementation
class SpecificExtendedForm(ExtendedForm):
    class ExtendedOptions:
        foo = 'quux'

Methods

options.options_factory

The main method that does all of the work is options.options_factory. It generates a new Metaclass that you should apply to your base class, allowing subclasses to define their own options. The method accepts four arguments:

options_processor

A callable that processes options the options dict from the class. This is usually a class subclassing options.OptionsBase, but could be any callable as long as it returns some options structure.

options_class_name

The name of the attribute on the class where the options definition can be found.

Defaults to the options_processor.__name__, which works well with classes.

options_attr_name

The name of the attribute where processed options will be stored.

Defaults to '_' + options_class_name.lower().

metaclass

The base metaclass to extend. Many Django classes have metaclasses already, so if you’re adding options to class that already has a metaclass, you must supply it here for the class to work properly.

Defaults to type.

options.form_options_factory

Calls options.options_factory with the metaclass already set to the metaclass for Django forms.

options.modelform_options_factory

Calls options.options_factory with the metaclass already set to the metaclass for Django model forms.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-easyoptions-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file django-easyoptions-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-easyoptions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7cf29e7097f84f8a319624b082d34b4eda19d2ebf80f3315c37eda7e38a5fa28
MD5 048a0fcf7633118d2b41a2098f4d6e3a
BLAKE2b-256 00a557f22358c12d1af03d2de004b725e4eeb64e4f3159aa6d1d98586c2d88b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page