Welcome to Django Fieldmaker’s documentation!¶

This package enables you to design forms in the Django admin. These forms can be used in your code or to extend existing forms in the admin itself. Other libraries may register new fields or widgets for the designer to use.

Installation¶

Requirements¶

  • Python 2.5 or later
  • Django 1.3

Settings¶

Put ‘fieldmaker’ into your INSTALLED_APPS section of your settings file.

Using Expandable Forms¶

ExpandableForm and ExpandableModelForm allow you to define forms and have the user expand those forms through the admin. The form will add any fields defined in the form definition having the form_key specified in the Meta. The ExpandableModelForm will additionally save the extra information and associate it to the instance.

Example usage:

from django import forms
from fieldmaker.forms import ExpandableForm, ExplandableModelForm
from myapp.models import MyModel

class MyForm(ExpandableForm):
    title = forms.CharField()

    class Meta:
        form_key = 'myform'

class MyModelForm(ExpandableModelForm):
    class Meta:
        model = MyModel
        form_key = 'mymodel'

Extending Your Admin¶

ExpandableModelAdmin allows for forms in the admin to have fields dynamically defined and added to them. If a ModelAdmin that inherits from this class is registered in the admin, then creating a form definition with the key <app_label>_<object_name> and adding fields will add fields you your admin.

Adding a form definition using the admin with the key “myapp_mymodel” would add dynamically fields to the MyModel admin below:

from django.contrib import admin
from fieldmaker.admin import ExpandableModelAdmin

from myapp.models import MyModel

class MyModelAdmin(ExpandableModelAdmin):
    pass

admin.site.register(MyModel, MyModelAdmin)

Meta Form Fields¶

FormField¶

FormField is a django field that allows you to embed a form as a field. To work properly, the form must inherit from MetaForm (or use the MetaFormMixin).

Example usage:

from django import forms
from spec_widget import FormField, MetaForm

class PersonForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()

class PeopleForm(MetaForm):
    person_one = FormField(form=PersonForm)
    person_two = FormField(form=PersonForm)

form = PeopleForm(data=data)
if form.is_valid():
    print form.cleaned_data

ListFormField¶

ListFormField works like FormField but instead allows for an array of objects. This works by producing a formset and using that as the form.

Example usage:

from django import forms
from spec_widget import ListFormField, MetaForm

class PersonForm(forms.Form):
    first_name = forms.CharField()
    last_name = forms.CharField()

class GroupForm(MetaForm):
    name = forms.CharField()
    people = ListFormField(form=PersonForm)

form = PeopleForm(data=data)
if form.is_valid():
    print form.cleaned_data

Using FacetField¶

FacetField is used to associate generic data to an instance. A model may have more then one FacetField provided they each are passed a different facet key. If you use an ExpandableModelAdmin or an ExpandableModelForm then one can add a FacetField to allow for easy access of that data.

Example usage:

from django.db import models
from django.core.files import File
from fieldmaker.modelfields import FacetField

class MyModel(models.Model):
    attributes = FacetField()


mymodel = MyModel.objects.get(pk=1)
mymodel.attributes['foo'] = 'bar'
mymodel.attributes['somefile'] = File(open('/path/to/file.txt', 'r'))
mymodel.attributes.save()

Extending Fieldmaker¶

To register a new field:

from fieldmaker.fields import BaseFieldForm, BaseField
from fieldmaker.resources import field_registry

class URLFieldForm(BaseFieldForm):
    max_length = forms.IntegerField(required=False)
    min_length = forms.IntegerField(required=False)
    verify_exits = forms.BooleanField(initial=False, required=False)
    validator_user_agent = forms.CharField(required=False)

class URLField(BaseField):
    form = URLFieldForm
    field = forms.URLField
    identities = ['URLField']

field_registry.register_field('URLField', URLField)

To register a new widget:

from fieldmaker.widgets import BaseWidgetForm, BaseWidget
from fieldmaker.resources import field_registry

class PasswordInputWidgetForm(BaseWidgetForm):
    render_value = forms.BooleanField(required=False, initial=True)

class PasswordInput(BaseWidget):
    widget = widgets.PasswordInput
    identities = ['CharField']

field_registry.register_widget('PasswordInput', PasswordInput)

Contrib¶

fieldmaker.contrib contains additional django apps that extend the functionality of django fieldmaker.

Recaptcha¶

fieldmaker.contrib.recaptcha adds a Recaptcha field (http://www.google.com/recaptcha). Add this to your INSTALLED_APPS and the RecaptchaField will be made available to the form definition. The field itself will validate the user input and will invalidate the form until the proper response is entered.

Download: http://github.com/zbyte64/django-fieldmaker

Indices and tables¶