Skip to main content

Installation

Installation is easy using pip or easy_install.

pip install django-formfield

or

easy_install django-formfield

Add to installed apps

INSTALLED_APPS = (
    ...
    formfield,
    ...
)

Getting Started

django-formfield is a form field that accepts a django form as its first argument, and validates as well as render’s each form field as expected. Yes a form within a form, within a dream? There are two types of fields available, FormField and ModelFormField. For ModelFormField the data is stored in json format. For FormField data is simply returned as a python dictionary (form.cleaned_data)

Example

from django.db import models
from django import forms

from formfield import ModelFormField

class PersonMetaForm(forms.Form):
    age = forms.IntegerField()
    sex = forms.ChoiceField(choices=((1, 'male'), (2, 'female')), required=False)


class Person(models.Model):
    name = CharField(max_length=200)

    meta_info = ModelFormField(PersonMetaForm)

Which will result in something like this (using the admin)

https://github.com/josesoa/django-formfield/raw/master/docs/_images/ss001.png

The ModelFormField is automatically set to null=False, blank=False, this is because validation is done on the inner form. As a result you will see something like the following if we hit save on the change form:

https://github.com/josesoa/django-formfield/raw/master/docs/_images/ss002.png

If we supply the change for valid data you should get a python dictionary when retrieving the data:

>>> person = Person.objects.get(pk=1)
>>> person.meta_info
{u'age': 12, u'sex': u'1'}

The form is the only thing forcing input of the expected values, behind the scenes the data is being serialized into json. Therefore on the python level we can supply meta_info any valid json::

>>> from sample_app.models import Person
>>> data = {'some': 'thing', 'is': 'wrong', 'here': 'help!'}
>>> p = Person.objects.create(name="Joan", meta_info=data)
>>> p.meta_info
{'is': 'wrong', 'some': 'thing', 'here': 'help!'}

Form within a form within a form within a form within a form…..

Sure its possible..

https://github.com/josesoa/django-formfield/raw/master/docs/_images/ss003.png

Download files

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

Source Distribution

django-formfield-0.1.tar.gz (680.9 kB view details)

Uploaded Source

File details

Details for the file django-formfield-0.1.tar.gz.

File metadata

  • Download URL: django-formfield-0.1.tar.gz
  • Upload date:
  • Size: 680.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-formfield-0.1.tar.gz
Algorithm Hash digest
SHA256 9376367773971e4eed9bb8519cffdb3a0b17418ce9a57ac6d566360870383790
MD5 01334a0721215af0237edb429d34e66d
BLAKE2b-256 b4d58f10655f6c2fcd9e77e00499d09044d6502f1fde710e3d6de04331f32a8f

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4

1 file

0.3

1 file

0.2

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

This release

0.1 This release

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page