Skip to main content

Dynamic Django forms and filters using JSON schema

Project description

Capybara forms

Dynamic Django forms and filters using JSON schema.

If you have 2 models: Category and Entry. And some entry features depends on it's category. Using this library, you will obtain easy-to-configure schema with form, filter and entry fields generators. Also, it can work with model fields.

Quick start

1. Define your models

from django.db import models
from capybara_forms.models import CapybaraFormsCategory, CapybaraFormsModel

class Category(CapybaraFormsCategory):
    title = models.CharField(max_length=100)

class Advert(CapybaraFormsModel(Category)):
    # Global model fields
    title = models.CharField(max_length=100)
    price = models.PositiveSmallIntegerField()
    # Rest fields will be stored in JSON 'data' field

2. Write schema for category 'Cats'

    "name": "breed",
    "type": "string",
    "required": true,
    "display_name": "Breed",
    "placeholder": "Enter breed of your cat"
    "name": "year",
    "type": "number",
    "required": true,
    "max_value": 2020,
    "min_value": 1980,
    "placeholder": "Enter birth year of your cat",
    "display_name": "Birth year"
    "name": "weight",
    "type": "number",
    "placeholder": "Enter the weight of your cat",
    "display_name": "Weight"
    "name": "height",
    "type": "number",
    "placeholder": "Height (only placeholder, without display name)"
    "type": "color",
    "display_name": "Cat's color"
    "name": "name",
    "type": "string",
    "placeholder": "Enter cat nickname",
    "display_name": "Name"
    "name": "vaccinated",
    "type": "checkbox",
    "display_name": "is vaccinated"

3. Create form. It's a basic rendering object

from django import forms
from capybara_forms.forms import CapybaraFormsModelForm
from .models import Advert

class AdvertForm(CapybaraFormsModelForm):
    # This fields from model will be rendered using form.render_form
    fields_in_model = ['title', 'price']

    # For custom placeholder
    title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter title'}))

    class Meta:
        model = Advert
        fields = ('title', 'price')

4. Use this form in your view

from django.shortcuts import render, get_object_or_404, redirect

from .models import Category
from .forms import AdvertForm

def add(request, category_id):
    category = get_object_or_404(Category, pk=category_id)
    if request.method == 'POST':
        form = AdvertForm(category, request.POST.dict())
        if form.is_valid():
            return redirect('/{0}/'.format(category_id))
        form = AdvertForm(category)

    return render(request, 'add.html', {
        'form': form,

5. Render form in your template. Note, that library uses parsley for validation. All dependencies listed below

<form method="POST" data-parsley-validate>
    {% csrf_token %}
    {{ form.render_form | safe }}

    <button type="submit">Submit</button>

Result form:
Form image

Result filter, with similar schema, plus some in-form configuration:
Filter image


  1. Django
  2. jQuery (version 3.3.1 included)
  3. JSON editor (version 5.9.6 included)
  4. parsley (version 2.8.1 included)

Documentation and full example

See readthedocs for API, advanced usage cases, parameters definition and more information

See example project for all features, used in worked application


With goal to simplify, with love to capybaras

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

capybara_forms-0.0.2.tar.gz (234.4 kB view hashes)

Uploaded Source

Built Distribution

capybara_forms-0.0.2-py3-none-any.whl (244.3 kB view hashes)

Uploaded Python 3

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